安卓百分比布局之RelativeLayout

来源:互联网 发布:淘宝网婴幼儿童车 编辑:程序博客网 时间:2024/06/02 05:13
public class PencentLayout extends RelativeLayout {    public PencentLayout(Context context) {        super(context);    }    //这下解析XML文件然后把获取到的属性赋给控件    @Override    public LayoutParams generateLayoutParams(AttributeSet attrs) {        return new LayoutParams(getContext(),attrs);    }    //测量宽高,此处主要用于获得父容器的宽高    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int width= View.MeasureSpec.getSize(widthMeasureSpec);//        int height= View.MeasureSpec.getSize(heightMeasureSpec);        int count=getChildCount();//获得所有里面的子控件        for(int i=0;i<count;i++){            View child=getChildAt(i);//遍历每一个子控件            ViewGroup.LayoutParams layoutParams=child.getLayoutParams();//获得每一个子控件布局参数集合            float widthpercent=0;            float heightpercent=0;            if (layoutParams instanceof PencentLayout.LayoutParams ){//判断此类是否是 LayoutParams的实例                widthpercent=((LayoutParams) layoutParams).getWidthPercent();//如果是就获取他的宽度和高度                heightpercent=((LayoutParams) layoutParams).getHeightPercent();            }            if (widthpercent!=0){//如果子控件没有使用这个属性责不进行修改                layoutParams.width= (int) (width*widthpercent);//修改子控件的宽度为父容器的宽度的百分比            }            if (heightpercent!=0){                layoutParams.height= (int) (height*heightpercent);            }        }        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }    public static class LayoutParams extends RelativeLayout.LayoutParams{        private float widthPercent;//在布局参数里面多加上自己自定义的参数        private float heightPercent;        public float getWidthPercent() {            return widthPercent;        }        public float getHeightPercent() {            return heightPercent;        }        public LayoutParams(Context c, AttributeSet attrs) {            super(c, attrs);            TypedArray array=c.obtainStyledAttributes(attrs,R.styleable.percentLayout);//获取所有参数集合            heightPercent=array.getFloat(R.styleable.percentLayout_layout_heightPercen,heightPercent);//获取自定义好的参数的值            widthPercent=array.getFloat(R.styleable.percentLayout_layout_widthPercen,widthPercent);//同上            array.recycle();        }    }

}

attr属性文件:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="percentLayout">        <attr name="layout_widthPercen" format="float"></attr>        <attr name="layout_heightPercen" format="float"></attr>    </declare-styleable></resources>


布局:

<com.example.administrator.myapplication.PencentLayout    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"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" >    <TextView        android:text="@string/hello_world"        android:layout_width="wrap_content"        app:layout_heightPercen="0.5"        app:layout_widthPercen="0.5"        android:layout_centerHorizontal="true"        android:gravity="center"        android:background="#ff0000"        android:layout_height="wrap_content" /></com.example.administrator.myapplication.PencentLayout>


0 0
原创粉丝点击