Android 百分比布局

首先引入依赖:
compile ‘com.android.support:percent:25.3.1’

包中可用的布局为 PercentRelativeLayout 和 PercentFrameLayout 。
PercentRelativeLayout 继承自 RelativeLayout, PercentFrameLayout继承自 FrameLayout 。

可用属性:
layout_widthPercent   宽度

layout_heightPercent  高度

layout_marginPercent  留白

layout_marginLeftPercent 左侧留白

layout_marginTopPercent  顶部留白

layout_marginRightPercent 右侧留白

layout_marginBottomPercent 底部留白

layout_marginStartPercent 左侧留白

layout_marginEndPercent  右侧留白

layout_aspectRatio  宽和高的比例
PercentRelativeLayout 使用
<?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">

    <!--左上右下留白 2%-->
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#88ff0000"
        android:text="高度 40%   宽度 40%"
        app:layout_heightPercent="40%"
        app:layout_widthPercent="40%"
        app:layout_marginLeftPercent ="2%"
        app:layout_marginRightPercent ="2%"
        app:layout_marginTopPercent ="2%"
        app:layout_marginBottomPercent ="2%"
        android:gravity="center"
        />
    <!--左上右下留白 2%-->
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#8800ff00"
        android:text="高度 40%   宽度 50%"
        android:layout_alignParentRight="true"
        app:layout_heightPercent="40%"
        app:layout_widthPercent="50%"
        app:layout_marginPercent= "2%"
        android:gravity="center"
        />

    <!--左侧和右侧留白5%-->
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:background="#880000ff"
        android:text="宽度:100%,高度:50%"
        app:layout_marginStartPercent = "5%"
        app:layout_marginEndPercent = "5%"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="100%"/>


</android.support.percent.PercentRelativeLayout>

效果如图:
1.png

layout_aspectRatio属性

app:layout_aspectRatio = “200%” 表示宽和高的比例位2:1

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="#8800ff00"
        android:text="宽度:50%,宽高比例为2:1"
        app:layout_widthPercent="50%"
        app:layout_aspectRatio = "200%"
        app:layout_marginPercent = "5%"
        />

效果如下:
2.png

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

    <TextView
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#88ff0000"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="80%"
        android:text="红"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#8800ff00"
        android:layout_gravity="center_horizontal|center_vertical"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%"
        android:text="绿"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#880000ff"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="蓝"/>
</android.support.percent.PercentFrameLayout>

如图:
3.png

android-percent-support-lib-sample
这个库实现了PercentLinearLayout。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值