片段如何在Android中使用

  介绍

本篇文章给大家分享的是有关片段如何在Android中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

片段初探

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

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

片段如何在Android中使用“> </p> <p>而如果现在程序运行在横屏模式的平板上,两个片段就可以嵌入在同一个活动中了,如下图所示:</p> <p> <img src= & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,,,android: layout_width=癿atch_parent"   ,,,android: layout_height=癿atch_parent"   ,,,android:背景=? 00 ff00",在   ,   ,,,& lt; TextView   ,,,,,,,android: layout_width=皐rap_content"   ,,,,,,,android: layout_height=皐rap_content"   ,,,,,,,android:文本=叭?能够is  fragment  1“;   ,,,,,,,android:输入textColor=? 000000“;   ,,,,,,,android: textSize=?5 sp",/比;   ,   & lt;/LinearLayout>

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

& lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,,,android: layout_width=癿atch_parent"   ,,,android: layout_height=癿atch_parent"   ,,,android:背景=? ffff00",在   ,   ,,,& lt; TextView   ,,,,,,,android: layout_width=皐rap_content"   ,,,,,,,android: layout_height=皐rap_content"   ,,,,,,,android:文本=叭?能够is  fragment  2“;   ,,,,,,,android:输入textColor=? 000000“;   ,,,,,,,android: textSize=?5 sp",/比;   ,   & lt;/LinearLayout>

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

public  class  Fragment1  extends  Fragment  {   ,   @Override   public  View  onCreateView (LayoutInflater 增压泵,ViewGroup 容器,Bundle  savedInstanceState), {   return  inflater.inflate (R.layout.fragment1,容器,假);   }   ,   }

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

public  class  Fragment2  extends  Fragment  {   ,   @Override   public  View  onCreateView (LayoutInflater 增压泵,ViewGroup 容器,Bundle  savedInstanceState), {   return  inflater.inflate (R.layout.fragment2,容器,假);   }   ,   }

然后打开或新建activity_main。xml作为主活动的布局文件,在里面加入两个片段的引用,使用android:名字前缀来引用具体的片段:

& lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,,,android: layout_width=癿atch_parent"   ,,,android: layout_height=癿atch_parent"   ,,,android: baselineAligned=癴alse",在   ,   ,,,& lt;片段   ,,,,,,,android: id=癅 + id/fragment1"   ,,,,,,,android: name=癱om.example.fragmentdemo.Fragment1"   ,,,,,,,android: layout_width=? dip"   ,,,,,,,android: layout_height=癿atch_parent"   ,,,,,,,android: layout_weight=?“,/比;   ,   ,,,& lt;片段   ,,,,,,,android: id=癅 + id/fragment2"   ,,,,,,,android: name=癱om.example.fragmentdemo.Fragment2"   ,,,,,,,android: layout_width=? dip"   ,,,,,,,android: layout_height=癿atch_parent"   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

片段如何在Android中使用