android软件百分比怎么实现,Android自实现百分比布局

在开发中,组件布局是大家每日开发必须要面对的工作,对于Android来说提供五种常用布局,分别是:LinearLayout(线性布局)

TableLayout(表格布局)

RelativeLayout(相对布局)

AbsoluteLayout(绝对布局)

FrameLayout(框架布局)

但是,开发中如果可以按照百分比的方式进行界面布局,将会对我们的适配工作带来许多便利。前段时间,谷歌正式提供百分比布局支持库(android-support-percent-lib),对于我们开发者来讲只需要导入这个库就可以实现百分比布局。现在我们抛开谷歌库不谈,自己其实也可以实现百分比布局。/**

*

* @ClassName: PercentRelativeLayout

* @Description: 自定义百分比相对布局

* @author 猴子搬来的救兵http://blog.csdn.net/mynameishuangshuai

*/public class PercentRelativeLayout extends RelativeLayout{

public PercentRelativeLayout(Context context) {        super(context);

}    public PercentRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);

}    public PercentRelativeLayout(Context context, AttributeSet attrs) {        super(context, attrs);

}    /**

* 重写测量方法

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        // 先拿到父控件的宽高

int width = View.MeasureSpec.getSize(widthMeasureSpec);        int height = View.MeasureSpec.getSize(heightMeasureSpec);        int count = this.getChildCount();        for (int i = 0; i 

View child = this.getChildAt(i);// 取出每一个子控件

ViewGroup.LayoutParams lp = child.getLayoutParams();            float widthPercent = 0;            float hightPercent = 0;            if (lp instanceof PercentRelativeLayout.LayoutParams) {// 支持百分比布局

widthPercent = ((PercentRelativeLayout.LayoutParams) lp).widthPercent;

hightPercent = ((PercentRelativeLayout.LayoutParams) lp).heightPercent;

}            if (widthPercent != 0) {                // 父容器的宽*宽的百分比

lp.width = (int) (width * widthPercent);

}            if (hightPercent != 0) {                // 父容器的高*高的百分比

lp.height = (int) (height * hightPercent);

}

}        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}    /**

* 重写对子控件布局方法

*/

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {        super.onLayout(changed, l, t, r, b);

}    /**

* 重写对子控件布局属性进行获取解析

*/

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {//      return super.generateLayoutParams(attrs);// 这里必须返回下面自定的LayoutParams

return new LayoutParams(getContext(), attrs);

}    public static class LayoutParams extends RelativeLayout.LayoutParams{

private float widthPercent;        private float heightPercent;        public LayoutParams(Context c, AttributeSet attrs) {            super(c, attrs);

TypedArray a = c.obtainStyledAttributes(attrs,R.styleable.precentRelativeLayout);

widthPercent = a.getFloat(R.styleable.precentRelativeLayout_layout_widthPrecent, widthPercent);

heightPercent = a.getFloat(R.styleable.precentRelativeLayout_layout_heightPrecent, heightPercent);

a.recycle();

}        public LayoutParams(int w, int h) {            super(w, h);

}        public LayoutParams(android.view.ViewGroup.LayoutParams source) {            super(source);

}        public LayoutParams(android.widget.RelativeLayout.LayoutParams source) {            super(source);

}

}

}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394

自定义属性文件:<?xml  version="1.0" encoding="utf-8"?>

1234567

布局文件:

xmlns:app="http://schemas.android.com/apk/res/com.castiel.demo"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

app:layout_heightPrecent="0.2"

app:layout_widthPrecent="0.2"

android:background="#28FF28"

android:text="http://blog.csdn.net/mynameishuangshuai" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

app:layout_heightPrecent="0.3"

app:layout_widthPrecent="0.3"

android:background="#28FF28"

android:text="猴子搬来的救兵" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

app:layout_heightPrecent="0.2"

app:layout_widthPrecent="0.2"

android:background="#28FF28"

android:text="castiel" />

123456789101112131415161718192021222324252627282930313233

运行结果:

AAffA0nNPuCLAAAAAElFTkSuQmCC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值