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.效果