第四章:手机平板要兼顾--探究碎片
4.1 碎片是什么
碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间,因而在平板上应用的非常广泛。
它和活动很像,同样都能包含布局,同样都有自己的生命周期。
平板上:新闻标题界面和新闻详细内容界面分别放在两个碎片中,然后在同一个活动中引入这两个碎片。
4.2 碎片的使用方式
碎片通常都是在平板上使用的,首先应该创建一个平板模拟器。
4.2.1 碎片的简单用法
//在一个活动中添加两个碎片
//左侧碎片布局 left_fragment.xml
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"
/>
</LinearLayout>
//这个布局,只放置了一个按钮,并让她水平居中显示。
//新建右侧碎片布局
//right_fragment.xml
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="00ff00" //绿色
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"
/>
</LinearLayout>
注意:Fragment有两种
1)android.app.Fragment 系统内置的
2)android.support.v4.app.Fragment support-v4库中的 (建议使用)(这里不需要添加依赖就可以使用,因为build.gradle已经添加了appcompat-v7库的依赖,这个库中包含support-v4)
//新建一个LeftFragment类,并让他继承自Fragment
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
//同样再新建一个RightFragment
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
注意:可以通过android:name来制定碎片的类名,一定要把包名也加上
//接下来修改activity_main中的代码(活动)
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
4.2.2 动态添加碎片
碎片的真正强大之处在于它可以在程序运行时动态的添加到活动中
//在以上代码中继续完善,新建another_right_fragment.xml
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="ffff00"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is another right fragment"
/>
</LinearLayout>
//新建一个AnotherRightFragment类作为另一个右侧碎片
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment,container,false);
return view;
}
}
//接下来修改activity_main中的代码(活动)
<LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android: