碎片
为了使页面在大平板中更加适应,android自3.0版本开始引入碎片。
碎片是一种可以嵌在活动中的一种UI片段,可以使程序更加合理的利用大屏空间,可以将碎片理解为一种迷你活动,但是拥有自己的生命周期与活动事件。
在手机应用开发过程中,使用碎片能使实现一个界面的多次利用。
碎片的简单使用
- 静态的使用方法
(1)创建fragment的布局文件xml
(2)自定义类继承自Fragment,将布局动态的加载进来
(3)在MainActivity 的布局中使用标签 < fragment >添加碎片,【通过android:name的属性来显式指明要添加的碎片类名,一定要将包名带上】
左布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="左按钮"
android:textSize="20dp"/>
</LinearLayout>
右布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="右按钮"
android:textSize="20dp"/>
</LinearLayout>
LeftFragment继承Fragment
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container