说明:本文仅为简单思路,没有实现项目适用的轮子
通过百分比布局做屏幕适配的主要思路是:
以父容器尺寸做为参考,在View的加载过程,根据当前父容器实际
尺寸换算出目标尺寸,再作用在View上。
先梳理下大概步骤:
- 自定义属性定义一些百分比的属性值
- 创建自定义View继承自
RelativeLayout
- 解析自定义属性
- 重写自定义View的
onMeasure()
方法 实现百分比布局适配
下面依次来看 :
自定义属性
在values
文件夹下新建arrts.xml
文件并声明以下自定义属性:
<declare-styleable name="PercentLayout">
<attr name="widthPercent" format="float" />
<attr name="heightPercent" format="float" />
<attr name="marginLeftPercent" format="float" />
<attr name="marginRightPercent" format="float" />
<attr name="marginTopPercent" format="float" />
<attr name="marginBottomPercent" format="float" />
</declare-styleable>
创建自定义View
这步比较简单,就是创建自定义View继承自RelativeLayout
,重写构造方法,和onMeasure()
方法,不在赘述.代码看后面附上的完整代码~
解析自定义属性
说明一下:
- 这里仿照了
RelativeLayout
解析属性的方式,子饿了一个内部类,用于解析自定义属性 - 在Android的View加载过程中,View的
LayoutParams
属性是又父控件决定的,所以重写了LayoutParams generateLayoutParams(AttributeSet attrs)
方法,目的是让子控件构建的LayoutParams
属性对象是自定义View的内部类的LayoutParams
,竖起来有点绕,可以看下代码和注释,很好理解~
//子View调用该方法创建自身的LayoutParams对象,重写之后,创建的对象就是下面内部类,保证子View使用了自定义属性能够正常解析
public LayoutParams generateLayoutParams(AttributeSet attrs){
return new LayoutParams(getContext(), attrs);
}
//这里因为自定义View继承自RelativeLayout,直接仿照RelativeLayout解析自定义属性的方式,写了一个内部类用于解析自定义属性
public static class LayoutParams extends RelativeLayout.LayoutParams{
private float widthPercent;
private float heightPercent;
private float marginLeftPercent;
private float marginRightPercent;
private float marginTopPercent;
private float marginBottomPercent;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
//解析自定义属性
TypedArray a = c.obtainStyledAttributes(attrs,R.styleable.PercentLayout);
widthPercent = a.getFloat(R.styleable.PercentLayout_widthPercent, 0);
heightPercent = a.getFloat(R.styleable.PercentLayout_heightPercent, 0);
marginLeftPercent = a.getFloat(R.styleable.PercentLayout_marginLeftPercent, 0);
marginRightPercent = a.getFloat(R.styleable.PercentLayout_marginRightPercent, 0);
marginTopPercent = a.getFloat(R.styleable.PercentLayout_marginTopPercent, 0);
marginBottomPercent = a.getFloat(R.styleable.PercentLayout_marginBottomPercent, 0);
a.recycle();
}
}
重写自定义View的onMeasure()
方法 实现百分比布局适配
这里就直接贴完整代码了,看下备注,没什么难度~
public class PercentLayout extends RelativeLayout {
public PercentLayout(Context context) {
super(context);
}
public PercentLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PercentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取父容器的尺寸
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
ViewGroup.LayoutParams params = child.getLayoutParams();
//判断子View的LayoutParams属性是否等同于(instanceof ) 内部类LayoutParams对象
if (checkLayoutParams(params)){
//如果是自定义百分比的属性,就获取自定义的各个百分比值
LayoutParams lp = (LayoutParams)params;
float widthPercent = lp.widthPercent;
float heightPercent = lp.heightPercent;
float marginLeftPercent = lp.marginLeftPercent;
float marginRightPercent= lp.marginRightPercent;
float marginTopPercent= lp.marginTopPercent;
float marginBottomPercent = lp.marginBottomPercent;
//判断获取的自定义属性值,如果大于0,证明xml里进行了声明,并根据属性值进行调整View的宽高以及各方向的Margin值
if (widthPercent > 0){
params.width = (int) (widthSize * widthPercent);
}
if (heightPercent > 0){
params.height = (int) (heightSize * heightPercent);
}
if (marginLeftPercent > 0){
((LayoutParams) params).leftMargin = (int) (widthSize * marginLeftPercent);
}
if (marginRightPercent > 0){
((LayoutParams) params).rightMargin = (int) (widthSize * marginRightPercent);
}
if (marginTopPercent > 0){
((LayoutParams) params).topMargin = (int) (heightSize * marginTopPercent);
}
if (marginBottomPercent > 0){
((LayoutParams) params).bottomMargin = (int) (heightSize * marginBottomPercent);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
//校验属性是否是百分比布局属性
return p instanceof LayoutParams;
}
//子View会调用这个方法构建自身的LayoutParams属性,这里创建的就是内部类的LayoutParams对象
public LayoutParams generateLayoutParams(AttributeSet attrs){
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends RelativeLayout.LayoutParams{
private float widthPercent;
private float heightPercent;
private float marginLeftPercent;
private float marginRightPercent;
private float marginTopPercent;
private float marginBottomPercent;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
//解析自定义属性
TypedArray a = c.obtainStyledAttributes(attrs,R.styleable.PercentLayout);
widthPercent = a.getFloat(R.styleable.PercentLayout_widthPercent, 0);
heightPercent = a.getFloat(R.styleable.PercentLayout_heightPercent, 0);
marginLeftPercent = a.getFloat(R.styleable.PercentLayout_marginLeftPercent, 0);
marginRightPercent = a.getFloat(R.styleable.PercentLayout_marginRightPercent, 0);
marginTopPercent = a.getFloat(R.styleable.PercentLayout_marginTopPercent, 0);
marginBottomPercent = a.getFloat(R.styleable.PercentLayout_marginBottomPercent, 0);
a.recycle();
}
}
}
网友评论