Android自定义百分比布局

 1  首先作一个定义的View 继承RelativeLayout (举例用RelativeLayout 固然你能够用线性布局都无所谓)。android

    而后重写一下3个方法。下一步作过自定义属性动画的同窗都应该知道。咱们应该重写onMeasure()方法。在这个方法里面 咱们会作几个事情:1 获取到屏幕的宽高。2 拿到控件的宽高。而后进行改变。下面是代码。
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 测量高度
        int width = View.MeasureSpec.getSize(widthMeasureSpec);
        int height = View.MeasureSpec.getSize(heightMeasureSpec);
        // 测量自控件的宽高 而后进行改变
        int childCount = getChildCount();// 获取到下面的全部子控件
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            float widthpercent = 0;
            float heightpercent = 0;
            ViewGroup.LayoutParams Params = view.getLayoutParams();
            if (Params instanceof PercentLayout.LayoutParams) {
                widthpercent = ((PercentLayout.LayoutParams) Params)
                        .getWidthPercent();
                heightpercent = ((PercentLayout.LayoutParams) Params)
                        .getHeightPercent();
            }app

            if (widthpercent != 0) {
                Params.width = (int) (width * widthpercent);
                Params.height = (int) (height * heightpercent);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }ide

2 可是作完上面的仍是不够的。咱们还须要复写generateLayoutParams()布局

    @Override
    public android.widget.RelativeLayout.LayoutParams generateLayoutParams(
            AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }动画

3  如今咱们来写LayoutParams这个内部类.net

class LayoutParams extends RelativeLayout.LayoutParams {
        private float widthPercent;
        private float heightPercent;orm

        public LayoutParams(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            TypedArray array = context.obtainStyledAttributes(attributeSet,
                    R.styleable.percentlayout);
            heightPercent = array.getFloat(
                    R.styleable.percentlayout_layout_heightpercent,
                    heightPercent);
            widthPercent = array
                    .getFloat(R.styleable.percentlayout_layout_widthpercent,
                            widthPercent);
            array.recycle();// 释放掉
        }xml

注意的是咱们在这里继承的是这个RelativeLayout。若是你用LinLayout的也能够的 可是咱们要和这个自定义的保持一致。咱们作的是一个自定义RelativeLayout 的百分比布局。而后是把里面的方法都复写一次。继承

4 而后是布局文件。我自定义了一个attr文件utf-8

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable  name="percentlayout">
        <attr  name="layout_widthpercent"   format="float">0</attr>
        <attr  name="layout_heightpercent"  format="float">0</attr>
    </declare-styleable>
    </resources>
5  看activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<com.example.percentlayout.PercentLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.percentlayout"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:text="Nice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    app:layout_widthpercent="0.6"
    android:layout_marginLeft="20px"
    app:layout_heightpercent="0.8"
    android:background="@android:color/holo_orange_dark"
    />
</com.example.percentlayout.PercentLayout>

注意地方是那个app 这个属性是没有的。是咱们在头文件里面申明的固然 你也能够改为其余的

xmlns:app="http://schemas.android.com/apk/res/com.example.percentlayout"