首先,需要添加
com.android.support:percent:24.1.1 包,版本随意
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.1.1' compile 'com.android.support:percent:24.1.1'}
这个包给我们提供了
PercentRelativeLayout以及
PercentFrameLayout两种布局,
支持的属性有
layout_widthPercent
、
layout_heightPercent
、
layout_marginPercent
、
layout_marginLeftPercent
、
layout_marginTopPercent
、
layout_marginRightPercent
、
layout_marginBottomPercent
、
layout_marginStartPercent
、
layout_marginEndPercent
打开下载后的函数库,发现其中主要包含三个类。
PercentFrameLayout、PercentRelativeLayout、PercentLayoutHelper
大部分的百分比实现逻辑都在PercentLayoutHelper这个类中,首先我们需要知道,PercentFrameLayout、PercentRelativeLayout分贝继承于FrameLayout、RelativeLayout。原有的属性和方法都是可以使用的。同时PercentHelper还对其做了百分比布局的扩展,在xml文件中,增加了如下属性配置。
- heightPercent :百分比表示高度
- widthPercent :百分比表示宽度
- marginBottomPercent :百分比表示底部的间隔
- marginEndPercent:百分比表示距离最后一个View之间的间隔
- marginLeftPercent:百分比表示左边的间隔
- marginPercent :百分比表示View之间的间隔
- marginRightPercent:百分比表示右边的间隔
- marginStartPercent:百分比表示距离第一个View之间的间隔
- marginTopPercent:百分比表示顶部的间隔
下载地址: