首先引入依赖:
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>
效果如图:
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%"
/>
效果如下:
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>
如图:
android-percent-support-lib-sample
这个库实现了PercentLinearLayout。