fragment是一种可以嵌入活动当中的UI片段,主要是为了适应大屏幕的手机。
在as中提供了直接的创建fragment的方法,这里手动创建简单的fragment。
1.简单的创建fragment
创建fragment_left.XML文件,文件里面添加一个简单的button控件。代码如下

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <Button 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:id="@+id/button" 10 android:text="Button" 11 android:textSize="28dp" 12 android:layout_gravity="center" 13 /> 14 15 </LinearLayout>
按照相同的方法创建第二个文件fragment_right.XML,代码如下,我们把背景设置了颜色,以便区分。

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#00ff00" 6 > 7 8 <TextView 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:text="this is a fragment" 12 android:layout_gravity="center" 13 android:textSize="28sp" 14 /> 15 16 </LinearLayout>
接着我们创建LeftFragment类继承来自Fragment,可以直接使用as的创建fragment方法。new->Fragment->blankfragment,进入新的页面,此时注意不要勾线任何选项,入下图,点击finish完成。重写onCreate()类。
具体代码如下:

1 public class LeftFragment extends Fragment { 2 3 4 public LeftFragment() { 5 // Required empty public constructor 6 } 7 8 9 @Override 10 public View onCreateView(LayoutInflater inflater, ViewGroup container, 11 Bundle savedInstanceState) { 12 View view = inflater.inflate(R.layout.fragment_left,container,false); 13 return view; 14 } 15 16 }
同理,RightFragment.java 代码如下:

1 public class RightFragment extends Fragment { 2 3 4 public RightFragment() { 5 // Required empty public constructor 6 } 7 8 9 @Override 10 public View onCreateView(LayoutInflater inflater, ViewGroup container, 11 Bundle savedInstanceState) { 12 13 View view = inflater.inflate(R.layout.fragment_right,container,false); 14 return view; 15 } 16 17 18 }
修改activity_main.XML如下:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:orientation="horizontal" 8 tools:context=".MainActivity"> 9 10 <fragment 11 android:layout_width="0dp" 12 android:layout_height="match_parent" 13 android:layout_weight="1" 14 android:name="com.example.yuqt.simplefragment.LeftFragment" 15 android:id="@+id/fragment_left" 16 /> 17 18 <fragment 19 android:layout_width="0dp" 20 android:layout_height="match_parent" 21 android:layout_weight="1" 22 android:name="com.example.yuqt.simplefragment.RightFragment" 23 android:id="@+id/fragment_right" 24 /> 25 26 27 </LinearLayout>
编译运行就完成了最简单的Fragment。
2.动态加载Fragment控件
上面是将Fragment像其他的控件一样之间在XML文件中添加的,Fragment还可以动态加载,效果为点击button的时候,加载另外一个Fragment
下面就来实现以下,首先添加另外一个Fragment类,命名为fragment_anoher_right.XML,其他代码都不变,就是讲背景颜色改为黄色:
ACtivity_main.XML代码修改如下:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:orientation="horizontal" 8 tools:context=".MainActivity"> 9 10 <fragment 11 android:layout_width="0dp" 12 android:layout_height="match_parent" 13 android:layout_weight="1" 14 android:name="com.example.yuqt.simplefragment.LeftFragment" 15 android:id="@+id/fragment_left" 16 /> 17 <FrameLayout 18 android:layout_width="0dp" 19 android:layout_weight="1" 20 android:id="@+id/right_content" 21 android:layout_height="match_parent"></FrameLayout> 22 23 <fragment 24 android:layout_width="0dp" 25 android:layout_height="match_parent" 26 android:layout_weight="1" 27 android:name="com.example.yuqt.simplefragment.RightFragment" 28 android:id="@+id/fragment_right" 29 /> 30 31 32 </LinearLayout>
MainActivity.java代码修改如下:

1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_main); 4 Button button = (Button)findViewById(R.id.button); 5 button.setOnClickListener(new View.OnClickListener() { 6 @Override 7 public void onClick(View v) { 8 AnotherRightFragment an = new AnotherRightFragment(); 9 FragmentManager manager = getFragmentManager(); 10 FragmentTransaction transaction = manager.beginTransaction(); 11 transaction.replace(R.id.right_content,an); 12 transaction.commit(); 13 } 14 }); 15 16 17 }
动态加载Fragment的过程如下:
1.先创建待添加的Fragment实例,
2.用getFragmentManager()方法获取FragmentManager的实例
3.开启一个事务
4.在事务中添加要显示的Fragment
5.提交事务