【Android开发】之Fragment开发1

 

    一直知道Fragment很强大,但是一直都没有去学习,现在有些空闲的时间,所以就去学习了一下Fragment的简单入门。我也会把自己的学习过程写下来,如果有什么不足的地方希望大牛指正,共同进步!

 

  一、Fragment简介

    1.Fragment作为Activity界面的一部分组成出现;

    2.可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用;

    3.在Activity运行过程中,可以添加、移除或者替换Fragment(add()、remove()、replace());

    4.Fragment可以响应自己的输入事件,并且有自己的生命周期,当然,它们的生命周期直接被其所属的activity的生命周期影响。

 

    那我们为什么要用Fragment呢?主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计。平板电脑的屏幕要比手机的大得多,有更多的空间来放更多的UI组件,并且这些组件之间会产生更多的交互。我们可以把Fragment认为是“小的Activity”,Fragment更加简洁。

 

 

  二、Fragment的简单使用

    那我们就简单的显示2个Fragment为例来讲解一下。

    

    1.在XML中添加Fragment:

      新建Fragment1、Fragment2(注意:这里可能有2个包可以选择导入android.app.Fragment或android.support.v4.app.Fragment都是可以的,我这里选择使用了前者,但是两者使用时有区别的,在结尾中我会讲到):

      Fragment1代码:

 1 package com.example.fragment;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 
10 import com.example.fragmentdemo.R;
11 
12 public class Fragment1 extends Fragment {
13     @Override
14     public View onCreateView(LayoutInflater inflater, ViewGroup container,
15             Bundle savedInstanceState) {
16         Log.e("TAG", "in");
17         return inflater.inflate(R.layout.fragment1, container, false);
18     }
19 }
View Code

      Fragment2代码:

 1 package com.example.fragment;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 
 9 import com.example.fragmentdemo.R;
10 
11 public class Fragment2 extends Fragment {
12     @Override
13     public View onCreateView(LayoutInflater inflater, ViewGroup container,
14             Bundle savedInstanceState) {
15         return inflater.inflate(R.layout.fragment2, container, false);
16     }
17 }
View Code

      Fragment1的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     android:background="#FF69B4"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="center"
12         android:text="这是第一个Fragment" />
13 
14 </LinearLayout>
View Code

      Fragment2的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     android:background="#EECBAD"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="center"
12         android:text="这是第二个Fragment" />
13 
14 </LinearLayout>
View Code

      我们在activity_main.xml中添加两个Fragment,代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:baselineAligned="false" >
 5 
 6     <fragment
 7         android:id="@+id/fragment1"
 8         android:name="com.example.fragment.Fragment1"
 9         android:layout_width="wrap_content"
10         android:layout_height="match_parent"
11         android:layout_weight="1" />
12 
13     <fragment
14         android:id="@+id/fragment2"
15         android:name="com.example.fragment.Fragment2"
16         android:layout_width="wrap_content"
17         android:layout_height="match_parent"
18         android:layout_weight="1" />
19 
20 </LinearLayout>
View Code

      MainActivity代码如下:

 1 package com.example.fragmentdemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 public class MainActivity extends Activity {
 7 
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_main);
12     }
13 }
View Code

      然后运行工程就可以显示Fragment了,下面是效果图。

    

       

    2.动态添加Fragment:

      我们只需要修改MainActivity和activity_main.xml中的代码就可以了。

      activity_main.xml代码:                 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:baselineAligned="false"
 5     android:orientation="vertical" >
 6 
 7     <LinearLayout
 8         android:id="@+id/main"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:orientation="vertical" />
12 
13 </LinearLayout>
View Code

       MainActivity代码:

 1 package com.example.fragmentdemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 import com.example.fragment.Fragment2;
 7 
 8 public class MainActivity extends Activity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         getFragmentManager().beginTransaction()
15                 .replace(R.id.main, new Fragment2()).commit();
16     }
17 }
View Code

 

      然后运行工程就可以动态的显示Fragment了,下面是效果图。

 

  三、app包下和V4包下的Fragment的区别

    1、尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的;

    2、android.support.v4.app.Fragment:可以兼容到1.6的版本;

    3、关于这两个fragment使用<fragment>标签的问题:

      (1)app.fragment和v4.fragment都是可以使用<fragment>标签的只是在在使用的时候如果是app.fragment则没有什么特殊的地方继承Activity即可。

      (2)当v4.fragment使用<fragment>标签的时候就要特别注意了:当这个Activity的布局中有<fragment>标签的时候,这个Activity必须继承FragmentActivity,否则就会报错。此时如果不卜继成FragmentActivity的话 编译系统会把<fragment>认为是app包中的Fragment来处理。但是此时我们导入的是v4包中的FragmentAndroid官方文档中的Fragment的例子就是以app包中的Fragment来讲解的。

      (3)app包中关于Fragment的类和方法在V4包中都是有相应的对应的。

    4.使用v4.fragment要通过getSupportFragmentManager()方法调用FragmentManager而不是getFragmentManager()方法。

    转载自:http://blog.csdn.net/a465456465/article/details/10415211,感谢。

 

      

  上面就是Fragment的简单使用方法,Demo下载,下一节我会讲Fragment的详细使用。欢迎关注,我的CSDN博客地址:http://blog.csdn.net/u010049692/article/details/38919531

 

 

      

posted @ 2014-08-28 23:05  gether  阅读(1337)  评论(2编辑  收藏  举报