知识点目录
知识点回顾
一般手机的屏幕大小在3-6英寸之间;一般平板电脑的屏幕在7-10英寸之间。随着屏幕大小的不同,同样的界面在视觉效果上有较大的差异。为了同时兼顾手机和平板视觉效果,从Android3.0开始引入了碎片的概念,它可以让界面在平板上更好地展示。
4.1 碎片是什么
碎片(Fragment)是一种可以嵌入在活动当中的UI片段。可以将其理解为一种迷你型的活动。有时候我们为了充分利用屏幕的大小,可以将多个Fragment嵌入到一个活动当中。
4.2 碎片的使用方式
4.2.1 碎片的简单用法
1. 新建碎片布局文件
新建左侧碎片布局left_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"
android:textAllCaps="false"/>
</LinearLayout>
新建右侧碎片布局right_fragment.xml:
<LinearLayout xmlns: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>
2. 创建Fragment类
新建的LeftFragment类需要继承Fragment,Fragment有系统内置的android.app.Fragment和support-v4库中的android.support.v4.app.Fragment两种,考虑更好的兼容性,建
最低0.47元/天 解锁文章
955

被折叠的 条评论
为什么被折叠?



