Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)

来源:互联网 发布:天才麻将少女 知乎 编辑:程序博客网 时间:2024/05/03 15:47

Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)

注明:这个转的,见谅未能标明原始出处

我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述。

但是Activity也有它的局限性,同样的界面在手机上显示可能很好看,在平板上就未必了,因为平板的屏幕非常大,手机的界面放在平板上可能会有过分被拉 长、控件间距过大等情况。这个时候更好的体验效果是在Activity中嵌入"小Activity",然后每个"小Activity"又可以拥有自己的布 局。因此,我们今天的主角Fragment登场了。

Fragment初探

为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个 Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和 ImageView,来显示书的详细内容和图片。

如果现在程序运行竖屏模式的平板或手机上,Fragment 1可能嵌入在一个Activity中,而Fragment 2可能嵌入在另一个Activity中,如下图所示:

而如果现在程序运行在横屏模式的平板上,两个Fragment就可以嵌入在同一个Activity中了,如下图所示:

由此可以看出,使用Fragment可以让我们更加充分地利用平板的屏幕空间,下面我们一起来探究下如何使用Fragment。

首先需要注意,Fragment是在3.0版本引入的,如果你使用的是3.0之前的系统,需要先导入android-support-v4的jar包才能使用Fragment功能。

新建一个项目叫做Fragments,然后在layout文件夹下新建一个名为fragment1.xml的布局文件:

[html]view plaincopy
  1. "http://schemas.android.com/apk/res/android" 
  2. android:layout_width="match_parent" 
  3. android:layout_height="match_parent" 
  4. android:background="#00ff00"
  5. android:layout_width="wrap_content" 
  6. android:layout_height="wrap_content" 
  7. android:text="This is fragment 1" 
  8. android:textColor="#000000" 
  9. android:textSize="25sp"/> 
  10.  

可以看到,这个布局文件非常简单,只有一个LinearLayout,里面加入了一个TextView。我们如法炮制再新建一个fragment2.xml :

[html]view plaincopy
  1. "http://schemas.android.com/apk/res/android" 
  2. android:layout_width="match_parent" 
  3. android:layout_height="match_parent" 
  4. android:background="#ffff00"
  5. android:layout_width="wrap_content" 
  6. android:layout_height="wrap_content" 
  7. android:text="This is fragment 2" 
  8. android:textColor="#000000" 
  9. android:textSize="25sp"/> 
  10.  

然后新建一个类Fragment1,这个类是继承自Fragment的:

[java]view plaincopy
  1. publicclass Fragment1 extends Fragment {   
  2. @Override 
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {   
  4. return inflater.inflate(R.layout.fragment1, container, false);   
  5.     }   
  6. }   

我们可以看到,这个类也非常简单,主要就是加载了我们刚刚写好的fragment1.xml布局文件并返回。同样的方法,我们再写好Fragment2 :

[java]view plaincopy
  1. publicclass Fragment2 extends Fragment {   
  2. @Override 
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {   
  4. return inflater.inflate(R.layout.fragment2, container, false);   
  5.     }   
  6. }   

然后打开或新建activity_main.xml作为主Activity的布局文件,在里面加入两个Fragment的引用,使用android:name前缀来引用具体的Fragment:

[html]view plaincopy
  1. "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:id="@+id/fragment1" 
  6. android:name="com.example.fragmentdemo.Fragment1" 
  7. android:layout_width="0dip" 
  8. android:layout_height="match_parent" 
  9. android:layout_weight="1"/> 
  10. android:id="@+id/fragment2" 
  11. android:name="com.example.fragmentdemo.Fragment2" 
  12. android:layout_width="0dip" 
  13. android:layout_height="match_parent" 
  14. android:layout_weight="1"/> 
  15.  

最后打开或新建MainActivity作为程序的主Activity,里面的代码非常简单,都是自动生成的:

[java]view plaincopy
  1. publicclass MainActivity extends Activity {   
  2. @Override 
  3. protectedvoid onCreate(Bundle savedInstanceState) {   
  4. super.onCreate(savedInstanceState);   
  5.         setContentView(R.layout.activity_main);   
  6.     }   
  7. }   

现在我们来运行一次程序,就会看到,一个Activity很融洽地包含了两个Fragment,这两个Fragment平分了整个屏幕,效果图如下:

动态添加Fragment

你已经学会了如何在XML中使用Fragment,但是这仅仅是Fragment最简单的功能而已。Fragment真正的强大之处在于可以动态地添加到 Activity当中,因此这也是你必须要掌握的东西。当你学会了在程序运行时向Activity添加Fragment,程序的界面就可以定制的更加多样 化。下面我们立刻来看看,如何动态添加Fragment。

还是在上一节代码的基础上修改,打开activity_main.xml,将其中对Fragment的引用都删除,只保留最外层的LinearLayout,并给它添加一个id,因为我们要动态添加Fragment,不用在XML里添加了,删除后代码如下:

[html]view plaincopy
  1. "http://schemas.android.com/apk/res/android" 
  2. android:id="@+id/main_layout" 
  3. android:layout_width="match_parent" 
  4. android:layout_height="match_parent" 
  5. android:baselineAligned="false"
  6.  

然后打开MainActivity,修改其中的代码如下所示:

[java]view plaincopy
  1.     publicclass MainActivity extends Activity {   
  2.     @Override 
  3.     protectedvoid onCreate(Bundle savedInstanceState) {   
  4.     super.onCreate(savedInstanceState);   
  5.             setContentView(R.layout.activity_main);   
  6.             Display display = getWindowManager().getDefaultDisplay();   
  7.     if (display.getWidth() > display.getHeight()) {   
  8.                 Fragment1 fragment1 = new Fragment1();   
  9. getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();   
  10.             } else {   
  11.                 Fragment2 fragment2 = new Fragment2();   
  12. getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();   
  13.             }   
  14.         }   
  15.     }   

首先,我们要获取屏幕的宽度和高度,然后进行判断,如果屏幕宽度大于高度就添加fragment1,如果高度大于宽度就添加fragment2。动态添加Fragment主要分为4步:

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

2.开启一个事务,通过调用beginTransaction方法开启。

3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。

4.提交事务,调用commit方法提交。

现在运行一下程序,效果如下图所示:


0 0
原创粉丝点击