Android在3.0版本引入了Fragment(碎片)功能,它类似于Activity,可以像Activity一样包含布局,通过将Activity 的布局分散到fragment 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化,很巧妙的解决了不同分辨率手机上UI差异变化的问题。
Fragment是我们在单个Activity上要切换多个UI界面时,要显示的不同内容。模块化这些UI面板可以提供给其他Acitivity来使用,因此我们可以简单地把Fragment看成类似于TextView控件一样,可以被任意的Activity进行加载。
静态加载:
具体步骤如下:<1>.首先在res->layout下建立两个xml文件。
<2>.其次建两个类都继承Fragment,引入上面对应的xml布局文件。
<3>.最后就是在activity_main.xml中应用这两个布局文件。
代码如下:
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00cccc"
android:orientation="vertical" >
<TextView
android:id="@+id/frag1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="FragmentA"
android:textSize="30sp" />
</LinearLayout>
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFF"
android:orientation="vertical" >
<TextView
android:id="@+id/frag2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="FragmentB"
android:textSize="30sp" />
</LinearLayout>
FragmentA.class
package com.niuml.fragment;
import android.app.Fragment;
import android.os.Bundle;
// import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* 导入 android.support.v4.app.Fragment;会报错 java.lang.ClassNotFoundException:
* 应该导入android.app.Fragment;这个包
**/
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
FragmentB.class
package com.niuml.fragment;
import android.app.Fragment;
import android.os.Bundle;
// import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* 注意:导入 android.support.v4.app.Fragment;会报错 java.lang.ClassNotFoundException:找不到类
* 应该导入android.app.Fragment;这个包
**/
public class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_b, container, false);
}
}
activity_main.xml中应用这两个布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragmenta"
android:name="com.niuml.fragment.FragmentA"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragmentb"
android:name="com.niuml.fragment.FragmentB"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>