为了让界面能够在平板上更好地展现,Android在3.0版本引入了Fragment(碎片)功能,它很是相似于Activity,能够像 Activity同样包含布局。Fragment一般是嵌套在Activity中使用的,如今想象这种场景:有两个 Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和 ImageView,来显示书的详细内容和图片。html
AD:51CTO学院:IT精品课程在线看!java
咱们都知道,Android上的界面展现都是经过Activity实现的,Activity实在是太经常使用了,我相信你们都已经很是熟悉了,这里就再也不赘述。android
可是Activity也有它的局限性,一样的界面在手机上显示可能很好看,在平板上就未必了,由于平板的屏幕很是大,手机的界面放在平板上可能会有过度被拉长、控件间距过大等状况。这个时候更好的体验效果是在Activity中嵌入"小Activity",而后每一个"小Activity"又能够拥有本身的布局。所以,咱们今天的主角Fragment登场了。ide
为了让界面能够在平板上更好地展现,Android在3.0版本引入了Fragment(碎片)功能,它很是相似于Activity,能够像 Activity同样包含布局。Fragment一般是嵌套在Activity中使用的,如今想象这种场景:有两个 Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和 ImageView,来显示书的详细内容和图片。布局
若是如今程序运行竖屏模式的平板或手机上,Fragment 1可能嵌入在一个Activity中,而Fragment 2可能嵌入在另外一个Activity中,以下图所示:spa
而若是如今程序运行在横屏模式的平板上,两个Fragment就能够嵌入在同一个Activity中了,以下图所示:.net
由此能够看出,使用Fragment可让咱们更加充分地利用平板的屏幕空间,下面咱们一块儿来探究下如何使用Fragment。code
首先须要注意,Fragment是在3.0版本引入的,若是你使用的是3.0以前的系统,须要先导入android-support-v4的jar包才能使用Fragment功能。xml
新建一个项目叫作Fragments,而后在layout文件夹下新建一个名为fragment1.xml的布局文件:htm
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp"/>
能够看到,这个布局文件很是简单,只有一个LinearLayout,里面加入了一个TextView。咱们如法炮制再新建一个fragment2.xml :
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 2"
android:textColor="#000000"
android:textSize="25sp"/>
而后新建一个类Fragment1,这个类是继承自Fragment的:
publicclass Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
咱们能够看到,这个类也很是简单,主要就是加载了咱们刚刚写好的fragment1.xml布局文件并返回。一样的方法,咱们再写好Fragment2 :
publicclass Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
而后打开或新建activity_main.xml做为主Activity的布局文件,在里面加入两个Fragment的引用,使用android:name前缀来引用具体的Fragment:
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
android:id="@+id/fragment1"
android:name="com.example.fragmentdemo.Fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
android:id="@+id/fragment2"
android:name="com.example.fragmentdemo.Fragment2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
最后打开或新建MainActivity做为程序的主Activity,里面的代码很是简单,都是自动生成的:
publicclass MainActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
如今咱们来运行一次程序,就会看到,一个Activity很融洽地包含了两个Fragment,这两个Fragment平分了整个屏幕,效果图以下:
你已经学会了如何在XML中使用Fragment,可是这仅仅是Fragment最简单的功能而已。Fragment真正的强大之处在于能够动态地添加到 Activity当中,所以这也是你必需要掌握的东西。当你学会了在程序运行时向Activity添加Fragment,程序的界面就能够定制的更加多样化。下面咱们马上来看看,如何动态添加Fragment。
仍是在上一节代码的基础上修改,打开activity_main.xml,将其中对Fragment的引用都删除,只保留最外层的LinearLayout,并给它添加一个id,由于咱们要动态添加Fragment,不用在XML里添加了,删除后代码以下:
"http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
而后打开MainActivity,修改其中的代码以下所示:
[java]view plaincopy
publicclass MainActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();
} else {
Fragment2 fragment2 = new Fragment2