Android学习备忘笺02Fragment

  Android中Fragment可以将UI界面分成多个区块,一般静态动态添加Fragment。

01.新建Fragment实例

  一个Fragment实例包括两个部分:类对象布局文件(可视化部分)。

  类对象继承Fragment,重写onCreateView方法,而布局文件作为View实例参数传入。

  a.新建一个布局文件:frag.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     <TextView
 7         android:text="Look! What a beautiful fragment."
 8         android:textSize="40sp"
 9         android:textStyle="italic"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content" />
12     <ImageView
13         android:src="@mipmap/orange"
14         android:layout_width="wrap_content"
15         android:layout_height="300dp" />
16 
17 </LinearLayout>

 

  b.新建一个类:fragmentMain.java

 1 public class fragmentMain extends Fragment {
 2     @Nullable
 3     @Override
 4     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
 5         //必须重写onCreateView();
 6         //将Fragment的布局文件转成View实例
 7         View view= inflater.inflate(R.layout.frag,null);
 8         return view;
 9         //return super.onCreateView(inflater, container, savedInstanceState);
10     }
11 }

 

 

 

02.静态添加

  静态添是在布局文件activity_main.xml 中声明<fragment></fragment>标签,name属性来指定需要添加的Fragment类实例。

  

 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:padding="20dp"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7 
 8     <TextView
 9         android:textSize="40sp"
10         android:layout_marginBottom="20dp"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Fragment Demo"
14          />
15     
16     <fragment
17         android:id="@+id/fra"
18         android:name="com.xxxx.xxxx.xxxx.fragmentMain"
19         android:layout_width="match_parent"
20         android:layout_height="match_parent"/>
21 
22 </LinearLayout>

 

03.动态添加

  动态添加指通过代码添加,而不是在布局文件中声明<fragment></fragment>标签,但是此时需要一个容器装载Fragment实例。

  例如如下:activity_main.xml 将LiinearLayout作为容器,此时应为其添加id。

 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:padding="20dp"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7 
 8     <TextView
 9         android:textSize="40sp"
10         android:layout_marginBottom="20dp"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Fragment Demo"
14          />
15     <LinearLayout
16         android:id="@+id/fragment"
17         android:orientation="vertical"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content">
20 
21     </LinearLayout>
22 </LinearLayout>

  然后通过代码添加实例。

1 //获取FragmentManager实例
2 FragmentManager fragmentManager=getFragmentManager();
3 //开始事务
4 FragmentTransaction trans= fragmentManager.beginTransaction();
5 //替换在Fragment容器中的Fragment实例
6 trans.replace(R.id.fragment,new fragmentMain());
7 //提交
8 trans.commit();

 

!注意

  有时导入不是 android.app.FragmentManager;包而是 android.support.v4.app.Fragment;时,如果根据是否向下兼容的情况替换。

  如果是android.support.v4.app.Fragment;包,应该使用 getSupportFragmentManager();获取实例。

 

  未添加Fragment与添加Fragment的效果如下:

  未添加:

  添加:

  暂时结束咯。

 

posted @ 2017-06-22 14:56  亲爱的小树  阅读(141)  评论(0编辑  收藏  举报