Android 第十七课 碎片的简单用法及动态添加碎片
Fragment(碎片)是一種可以嵌入在活動(dòng)當(dāng)中的UI片段,它可以讓程序更加合理和充分的利用大屏幕的空間。碎片和活動(dòng)太像了,同樣都包含布局,都有自己的聲明周期,可以將碎片理解為一種迷你型的活動(dòng)。
新建FragmentTest項(xiàng)目。假設(shè)項(xiàng)目已經(jīng)建立完成。
新建一個(gè)左側(cè)布局left_fragment.xml,代碼如下:
只是放置了一個(gè)按鈕,并讓它水平居中顯示,然后:
新建右側(cè)碎片布局right_fragment.xml,代碼如下:
我們只是將這個(gè)布局的背景設(shè)置成了綠色,并放置了一個(gè)TextView用于顯示一段文本。
接著新建一個(gè)LeftFragment類,這個(gè)類繼承自Fragment,這里有兩個(gè)不同包下的Fragment供你選擇,我們要選擇support-v4庫中的Fragment,因?yàn)樗梢宰屗槠谒蠥ndroid 版本中保持功能的一致性。另外,我們不需要在build.gradle文件中添加support-v4庫的依賴,因?yàn)閎uild.gradle文件中已經(jīng)添加了appcompat-v7庫的依賴,而這個(gè)庫,會(huì)將support-v4庫也一并引入進(jìn)來。
現(xiàn)在編寫LeftFragment中的代碼,如下所示:
這里僅僅重寫了Fragment的onCreata()方法,然后在這個(gè)方法中通過LayoutInflater的inflate()方法將剛才定義的left_fragment布局動(dòng)態(tài)加載進(jìn)來,整個(gè)方法就簡(jiǎn)單明了。
我們?cè)傩陆ㄒ粋€(gè)RightFragment,代碼如下:
package com.example.fragmenttest;import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class RightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.right_fragment,container,false);return view ;}}基本山代碼都是相同的,接下里修改acitivity_main.xml中的代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><fragmentandroid:id="@+id/right_fragment"android:name ="com.example.fragmenttest.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout>我們使用了<fragment>標(biāo)簽在布局中添加碎片,其中指定的大多數(shù)屬性都是比較熟悉的,只不過通過android:name屬性來顯式指明要添加地碎片類名,注意一定要將類的包名也加上。
我們可以運(yùn)行一下程序:
兩個(gè)碎片平分了整個(gè)活動(dòng)地布局。
2、動(dòng)態(tài)添加碎片
碎片地真正地強(qiáng)大之處在于,它可以在程序運(yùn)行時(shí)動(dòng)態(tài)地添加到活動(dòng)當(dāng)中,根據(jù)具體情況來添加碎片,你就可以將程序界面定制的更加多樣化。
我們接著上一節(jié)的內(nèi)容,新建another_right_fragment.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:background="#ffff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is another right fragment"/></LinearLayout>這個(gè)布局文件和right_fragment.xml中的代碼基本相同,只是將背景色變成了黃色。
新建AnotherRightFragment作為另一個(gè)右側(cè)碎片,代碼如下:
現(xiàn)在將右側(cè)碎片替換成一個(gè)Fragment中,Fragment是Android中最簡(jiǎn)單的一種布局,所有控件默認(rèn)都會(huì)擺放在布局的左上角。
由于這里僅僅需要在布局里放入一個(gè)碎片,不需要任何定位,非常適合使用FragmentLayout。
我們?cè)诖a中向Fragment中添加內(nèi)容,從而實(shí)現(xiàn)動(dòng)態(tài)添加碎片的功能。修改MainActivity中的代碼,如下:
package com.example.fragmenttest;import android.app.Fragment; import android.app.FragmentTransaction; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Button button =(Button)findViewById(R.id.button);button.setOnClickListener(this);replaceFragment(new RightFragment());} @Overridepublic void onClick(View view) {switch (view.getId()){case R.id.button:replaceFragment(new AnotherRightFragment());break;default:break;}}private void replaceFragment(android.support.v4.app.Fragment fragment){FragmentManager fragmentManager = getSupportFragmentManager();android.support.v4.app.FragmentTransaction tranction = fragmentManager.beginTransaction();tranction.replace(R.id.right_layout,fragment);tranction.commit();} }首先我們給左側(cè)碎片中的按鈕注冊(cè)了一個(gè)點(diǎn)擊事件,然后調(diào)用replaceFragment()方法動(dòng)態(tài)的添加Fragment這個(gè)碎片。當(dāng)點(diǎn)擊按鈕時(shí),又會(huì)調(diào)用replaceFragment()方法將右側(cè)碎片替換成AnotherRightFragment.結(jié)合replaceFramment()方法中的代碼可以看出,動(dòng)態(tài)添加碎片主要分為5步。
1、創(chuàng)建待添加的碎片實(shí)例
2、創(chuàng)建FragmentManager,在活動(dòng)中可以直接通過調(diào)用getSupportFragmentManager()方法得到。
3、開啟一個(gè)事務(wù),通過調(diào)用beginTransaction方法開啟。
4、向容器內(nèi)添加或替換碎片,一般使用replace()方法實(shí)現(xiàn),需要傳入容器的id和待添加的碎片實(shí)例。
5、提交事務(wù),調(diào)用commit()方法來完成。
運(yùn)行一下程序:點(diǎn)擊按鈕可以看到效果:
總結(jié)
以上是生活随笔為你收集整理的Android 第十七课 碎片的简单用法及动态添加碎片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二十三期:程序员节Keep被曝突然裁员
- 下一篇: Android 第十八课 强大的滚动控件