百分比布局PercentRelativeLayout - 简单使用

1.添加依赖

compile 'com.android.support:percent:25.3.1'

2.直接上代码

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

    <TextView
        android:id="@+id/top_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#1131d3"
        android:text="蓝色"
        android:textColor="#fff"
        android:gravity="center"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="70%"
        />

    <TextView
        android:id="@+id/top_right"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:layout_toRightOf="@+id/top_left"
        android:background="#a72525"
        android:text="红色"
        android:textColor="#fff"
        android:gravity="center"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="30%"
        />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/top_left"
        android:background="#3ed116"
        android:text="绿色"
        android:textColor="#fff"
        android:gravity="center"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="70%"
        />
    <!--
    引入: xmlns:app="http://schemas.android.com/apk/res-auto"
    尽管在layout_width中设置了match_parent,但是还是以app中widthpercent=50%为主要的
    除非没有widthpercent属性,才会以layout_width为主
    -->
    <!--
    android:layout_width和height可以不写
    -->

    <TextView
        android:id="@+id/right_botton"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_below="@+id/top_right"
        android:layout_toRightOf="@+id/bottom"
        android:background="#e1895e"
        app:layout_aspectRatio="50%"

        />

    <!--
    app:layout_aspectRatio="50%" 用来指定宽:高的比例,如上面宽等于100dp,所以求出高等于200dp
    -->
    <!--
    但是上述属性不能和下面的一起使用,会失效。下面两条属性占主导地位:
    app:layout_widthPercent="50%"
    app:layout_heightPercent="50%"
    -->

</android.support.percent.PercentRelativeLayout>

3.效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值