Android碎片机制

 

碎片

安卓系统不仅是手机的系统,同时在平板等其他的机型上运行,屏幕的尺寸也各不相同从3寸到10寸甚至更大(安卓电脑)或者更小(安卓手表),所以不同大小的屏幕会让一些界面在一些屏幕上过度拉伸或者元素的空隙过大等。影响用户的使用,所以Android引入了碎片的概念,他可以让界面在屏幕比较大的设备上显示的更加美观。

碎片的简单用法

新建两个布局文件 left_fragment.xmlright_fragment.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:orientation="vertical"
        android:background="#555"
        >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="center_horizontal"

        />
    </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">

        <TextView
            android:id="@+id/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>

新建两个类文件 LeftFragment.javaRightFragment.java

    public class LeftFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.left_fragment,container,false);
            return view;
        }
    }


    public class RightFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.right_fragment,container,false);
            return view;
        }
    }

更改MainActivity布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context="cn.edu.henu.rjxy.test4.MainActivity">

        <fragment
            android:id="@+id/left_fragment"
            android:name="cn.edu.henu.rjxy.test4.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <fragment
            android:id="@+id/right_fragment"
            android:name="cn.edu.henu.rjxy.test4.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />


    </LinearLayout>




查看原文: http://blog.democpp.cn/2018/03/27/android%e7%a2%8e%e7%89%87%e6%9c%ba%e5%88%b6/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值