一、概念
Percent Support Library是Google百分比布局兼容函数库,进行屏幕适配。
二、属性
使用android.support.percent.PercentRelativeLayout和android.support.percent.PercentFrameLayout分别替代掉RelativeLayout和FrameLayout。
现有以下属性可以使用:
layout_widthPercent : 宽度的百分比 ,例如:app:layout_widthPercent="25%"
layout_heightPercent : 高度的百分比
layout_marginPercent : 边距的百分比
layout_marginLeftPercent
layout_marginRightPercent
layout_marginTopPercent
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent
layout_aspectRatio : 用百分比来表示View的宽高比。
三、使用
使用Percent Support Library例子
首先需要在build.gradle文件中添加依赖:
dependencies {
compile 'com.android.support:percent:23.2.0'
}
代码如下:
<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">
<View
app:layout_widthPercent="25%"
android:layout_height="100dp"
app:layout_marginLeftPercent="5%"
android:background="#ff0000" />
</android.support.percent.PercentRelativeLayout>
不使用Percent Support Library例子
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="20">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="5"
android:background="#ff0000" />
</LinearLayout>
</RelativeLayout>