为了更好的与碎片化战斗,android团队终于推出百分比支持库
(sample下载地址:https://github.com/JulienGenoud/android-percent-support-lib-sample)
这个库是非常容易使用的,因为它就如同 RelativeLayout 和 FrameLayout 一样我们都熟悉,只是有一些额外的功能。
在gradle中添加依赖:
compile 'com.android.support:percent:23.0.0'
这个库提供了:
-
两种布局供大家使用:
PercentRelativeLayout
、PercentFrameLayout
,通过名字就可以看出,这是继承自FrameLayout
和RelativeLayout
两个容器类; -
支持的属性有:
layout_widthPercent
、layout_heightPercent
、
layout_marginPercent
、layout_marginLeftPercent
、
layout_marginTopPercent
、layout_marginRightPercent
、
layout_marginBottomPercent
、layout_marginStartPercent
、layout_marginEndPercent
。
简单的使用举例:
<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>