基本布局之 - PercentFrameLayout/PercentRelativeLayout

转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149395

基本布局之 - PercentFrameLayout/PercentRelativeLayout

百分比布局

1.1 PercentFrameLayout

前面介绍的3中布局都是从Android1.0版本开始支持了,一直沿用到现在,可以说满足了绝大多数场景的界面设计需求,不过LinearLayout支持使用layout_weight属性来实现按比例指定控件大小的功能,其它布局都不支持,比如说,如果想用RelativeLayout来实现让两个按钮平分布局宽度的效果,就必须使用Android引入的一种全新的布局方式来解决此问题—百分比布局,在这种布局中我们可以不再使用wrap_content/match_parent等方式来指定控件的大小,而是允许直接指定控件在布局中所占的百分比,这样的话就可以轻松实现平分布局甚至任意比例分割布局的效果了

由于LinearLayout本身已经支持按比例指定控件的大小了,因此百分比布局只为FrameLayout和RelativeLayout进行了功能扩展,提供了PercentFrameLayout和PercentRelativeLayout这两个全新的布局

百分比布局是新增布局,那么怎样才能做到让新增布局在所有Android版本上都能使用呢,为此,Android团队将百分比布局定义在support库当中,我们只需要在项目的build.gradle中添加百分比布局的依赖,就能保证百分比布局在Android所有系统版本上的兼容性

  • 最终效果
    这里写图片描述

  • 实例目录
    这里写图片描述

  • 在app/build.gradle文件的dependencies闭包中添加上百分比依赖

dependencies{
    ...
    compile 'com.android.support:percent:24.2.1'
    ...
}
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button 1"
        />

    <Button
        android:id="@+id/button2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button 2"
        />

    <Button
        android:id="@+id/button3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button 3"
        />

    <Button
        android:id="@+id/button4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button 4"
        />

</android.support.percent.PercentFrameLayout>

最外层我们使用PercentFrameLayout,由于百分比布局并不是内置在系统SDK中的,所有需要把完整的包路径写出来,探后必须定义一个app的命名空间,这样才能使用百分比布局的自定义属性

在PercentFrameLayout中我们定义了4个按钮,使用app:layout_widthPercent属性将各按钮的宽度指定为布局的50%,使用app:layout_heightPercent属性将各按钮的高度指定为布局的50%,这里之所以能使用app前缀的属性就是因为刚才定义了app的命名空间,当然我们一直使用android前缀属性也是同样的道理

不过PercentFrameLayout还是会继承FrameLayout的特性,即所有的控件默认都是摆放在布局的左上角,那个为了让这4个按钮不会重叠,这里还是借助了layout_gravity来分别将这4个按钮放置在布局的左上、右下、左下、右下4个位置

  • 最终效果
    这里写图片描述

可以看到,每一个按钮的宽和高都占据了布局的50%,这样我们就轻松实现了4个按钮平分屏幕的效果

1.2 PercentRelativeLayout

PercentRelativeLayout的用法也是非常相似的,它继承了RelativeLayout中的所有属性,并可以使用app:layout_widthPercent和app:layout_heightPercent来按百分比指定控件的宽高

  • 最终效果
    这里写图片描述

  • 修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button 1"
        />

    <Button
        android:id="@+id/button2"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button 2"
        />

    <Button
        android:id="@+id/button3"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Button 3"
        />
    <Button
        android:id="@+id/button4"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button 4"/>

</android.support.percent.PercentRelativeLayout>

使用PercentRelativeLayout就不能再使用android:layout_width/android:layout_height,必须使用app:layout_widthPercent/app:layout_heightPercent来控制控件的大小

转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149395

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值