深入浅析 Android Fragment(上篇)

2019-12-10 19:59:26王旭

是不是把Fragment当成普通的View一样声明在Activity的布局文件中,然后所有控件的事件处理等代码都由各自的Fragment去处理,瞬间觉得Activity好干净有木有~~代码的可读性、复用性以及可维护性是不是瞬间提升了~~~下面看下效果图:

深入浅析 Android Fragment(上篇)

4、动态的使用Fragment

上面已经演示了,最简单的使用Fragment的方式~下面介绍如何动态的添加、更新、以及删除Fragment

为了动态使用Fragment,我们修改一下Actvity的布局文件,中间使用一个FrameLayout,下面添加四个按钮~~~嘿嘿~~不是微信的按钮- -!

 

 
  1. <RelativeLayout xmlns:android="http://www.easck.com/apk/res/android"  xmlns:tools="http://www.easck.com/tools" 
  2. android:layout_width="match_parent"  android:layout_height="match_parent" >  
  3. <fragment   android:id="@+id/id_fragment_title" 
  4. android:name="com.zhy.zhy_fragments.TitleFragment"  android:layout_width="fill_parent" 
  5. android:layout_height="45dp" />   <include  
  6. android:id="@+id/id_ly_bottombar"  android:layout_width="fill_parent" 
  7. android:layout_height="55dp"  android:layout_alignParentBottom="true" 
  8. layout="@layout/bottombar" />   <FrameLayout  
  9. android:id="@+id/id_content"  android:layout_width="fill_parent" 
  10. android:layout_height="fill_parent"  android:layout_above="@id/id_ly_bottombar" 
  11. android:layout_below="@id/id_fragment_title" />   </RelativeLayout>