Android學習——片段Fragment的使用,
一、片段的簡單用法(實現在一個活動中添加兩個片段,並讓這兩個片段平分活動空間)
1、建立一個FragmentTest項目;
建立一個左側片段布局left_fragment.xml,代碼如下:(只放置一個按鈕並水平置中顯示)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 <Button 7 android:id="@+id/button" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content"10 android:layout_gravity="center_horizontal"11 android:text="Button"12 />13 </LinearLayout>
建立右側片段布局right_fragment.xml,代碼如下:(布局背景設定成藍色並放置一個TextView用於顯示文本)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@color/colorPrimaryDark"> 7 8 <TextView 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_gravity="center_horizontal"12 android:textSize="20sp"13 android:text="This is right fragment"14 />15 </LinearLayout>
2、建立一個LeftFragment類,並讓它繼承Fragment(注意這裡的Fragment使用support-v4中的),代碼如下:
1 public class LeftFragment extends Fragment{2 @Override3 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){4 View view=inflater.inflate(R.layout.left_fragment,container,false);5 return view;6 }7 }
重寫了Fragment的onCreateView()方法,然後在這個方法中通過LayoutInflater的inflate()方法將剛才定義的left_fragment布局動態載入進來。
建立RightFragment類,同樣繼承Fragment,代碼如下:
1 public class RightFragment extends Fragment {2 @Override3 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){4 View view=inflater.inflate(R.layout.right_fragment,container,false);5 return view;6 }7 }
3、修改activity_main.xml中的代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 <fragment 7 android:id="@+id/left_fragment" 8 android:name="com.example.administrator.fragmenttest.LeftFragment" 9 android:layout_width="0dp"10 android:layout_height="match_parent"11 android:layout_weight="1"12 />13 <FrameLayout14 android:id="@+id/right_layout"15 android:layout_width="0dp"16 android:layout_height="match_parent"17 android:layout_weight="1"18 >19 </FrameLayout>20 </LinearLayout>
運行程式,效果如下:
二、動態添加片段
1、在前面基礎上建立another_right_fragment.xml,代碼如下:(布局檔案和right_fragment.xml基本相同,僅修改了背景色和顯示文字)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="#ffff00" 7 > 8 <TextView 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_gravity="center_horizontal"12 android:textSize="20sp"13 android:text="This is another right fragment"14 />15 </LinearLayout>
2、建立AnotherRightFragment類作為另一個右側片段,代碼如下:
1 public class AnotherRightFragment extends Fragment{2 @Override3 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){4 View view=inflater.inflate(R.layout.another_right_fragment,container,false);5 return view;6 }7 }
3、修改activity_main.xml,代碼如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 <fragment 7 android:id="@+id/left_fragment" 8 android:name="com.example.administrator.fragmenttest.LeftFragment" 9 android:layout_width="0dp"10 android:layout_height="match_parent"11 android:layout_weight="1"12 />13 <FrameLayout14 android:id="@+id/right_layout"15 android:layout_width="0dp"16 android:layout_height="match_parent"17 android:layout_weight="1"18 >19 </FrameLayout>20 </LinearLayout>
4、修改MainActivity中的代碼:(在代碼中向FrameLayout裡新增內容)
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 Button button=(Button)findViewById(R.id.button); 8 button.setOnClickListener(this); 9 replaceFragment(new RightFragment());10 }11 12 @Override13 public void onClick(View v){14 switch(v.getId()){15 case R.id.button:16 replaceFragment(new AnotherRightFragment());17 break;18 default:19 break;20 }21 }22 23 private void replaceFragment(Fragment fragment){24 FragmentManager fragmentManager=getSupportFragmentManager();25 FragmentTransaction transaction=fragmentManager.beginTransaction();26 transaction.replace(R.id.right_layout,fragment);27 transaction.commit();28 }29 }
首先給左側片段中的按鈕註冊一個點擊事件,然後調用replaceFrameLayout()方法動態添加RightFragment這個片段,當點擊左側片段中的按鈕時,又會調用replaceFragment()方法將右側片段替換成AnotherRightFragment。
重新運行程式,可以看到和之前一樣的介面,然後點擊一下按鈕,效果如下: