MeasureSpec与自定义属性

来源:互联网 发布:dnz端口 编辑:程序博客网 时间:2024/06/08 05:49

自定义属性

步骤

        1,在values文件夹下建attrs.xml,用来存所有的自定义属性。如:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyView">        <attr name="xx" format="string"></attr>        <attr name="textColor" format="color" />        <attr name="textSize" format="dimension" />    </declare-styleable></resources>
        其中,一个自定义控件对应一个<declare-styleable>标签,(可以不用该标签)。其中的每一个<attr>标签是一个属性,name指的是属性名,format指的是属性的取值类型。

        2,在代码中,重写参数为Context与AttributeSet的构造方法,示例:

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SampleView);        for (int x = 0; x < array.length(); x++) {            int index = array.getIndex(x);            switch (index) {                case R.styleable.SampleView_isImage:                    array.getBoolean(index, false);                    break;                default:                    break;            }        }        array.recycle();

        Context.obtainStyledAttributes():将AttributeSet转换为TypedArray。它的第二个参数是一个int[],该方法得到了该数组中指定的属性的值。如果属性定义在attrs.xml中,可通过R.styleable.xxx进行引用。当然也可以自己写一个int[]数组。如:

private int[] att = { android.R.attr.divider, android.R.attr.showDividers };

        TypeArray.getIndex()得到的是在数组中的下标。通过比对该下标,就可以知道当前是哪个属性的值。

        在R文件中:

        public static final int[] SampleView = {            0x7f010000, 0x7f010001        };        public static final int SampleView_isImage = 1;        public static final int SampleView_showText = 0;    };    public static final class attr {        public static final int isImage=0x7f010001;        public static final int showText=0x7f010000;    }

       从上面可以看出,R.styleable.SampleView是一个int数组,它的元素就是我们在attrs.xml中自定义的属性,也就是R.attr中的值。而R.styleable.SampleView_xx等值,却是与相应属性在R.styleable.SampleView中的下标一致,而这些值也就是TypeArray.getIndex()返回的

使用

          对于自定义属性的使用,和平时系统自带的属性一样用法。只不过要重新声明一个名称空间。示例:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:my="http://schemas.android.com/apk/res/com.baigle.demo"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:weightSum="2" >    <com.baigle.demo.MyView        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="@android:color/darker_gray"        android:gravity="center"        my:textSize="15sp"        my:xx="fdaf" /></LinearLayout>
       其中xmlns:my便是声名名称空间。名称空间的组成为:http://schemas.android.com/apk/res-auto“本应用的包名”。

MeasureSpec

        在自定义View时,总少不了使用MeasureSpec类。从该类的方法可以看出它的作用:getSize()和getMode()。第一个是得到大小,第二个是得到模式。在MeasureSpec中有三种模式:EXACTLY(精确),AT_MOST(至多)与UNSPECIFIED(不确定)

        EXACTLY:精确,父类已经确定了子View应有的大小。一般子view在布局中指定layout_width和layout_height为确定值或者为match_parent时,mode为该值。

        AT_MOST:至多,子View不能超过该尺寸。一般当layout_width和layout_height为wrap_content时,mode为该值。

        UNSPECIFIED:不确定,一般当父View可以滚动时,子View的模式就是该值。

        对于一个View的MeasureSpec是由其本身的LayoutParams和父View的MeasureSpec共同决定的。这可以通过ViewGroup中的getChildMeasureSpec看出:

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {//这两个参数为本View的MeasureSpec,该方法的返回值将传递到子View的measure中        int specMode = MeasureSpec.getMode(spec);        int specSize = MeasureSpec.getSize(spec);        int size = Math.max(0, specSize - padding);//该View的剩余空间        int resultSize = 0;        int resultMode = 0;        switch (specMode) {        // Parent has imposed an exact size on us        case MeasureSpec.EXACTLY:            if (childDimension >= 0) {                resultSize = childDimension;                resultMode = MeasureSpec.EXACTLY;            } else if (childDimension == LayoutParams.MATCH_PARENT) {                // Child wants to be our size. So be it.                resultSize = size;                resultMode = MeasureSpec.EXACTLY;//父View给本View一个确定值,所以子View在MATCH_PARENT时也会得到一个确定值            } else if (childDimension == LayoutParams.WRAP_CONTENT) {                // Child wants to determine its own size. It can't be                // bigger than us.                resultSize = size;                resultMode = MeasureSpec.AT_MOST;            }            break;        // Parent has imposed a maximum size on us        case MeasureSpec.AT_MOST:            if (childDimension >= 0) {                // Child wants a specific size... so be it                resultSize = childDimension;                resultMode = MeasureSpec.EXACTLY;            } else if (childDimension == LayoutParams.MATCH_PARENT) {                // Child wants to be our size, but our size is not fixed.                // Constrain child to not be bigger than us.                resultSize = size;                resultMode = MeasureSpec.AT_MOST;//父View给本View一个AT_MOST,那在MATCH_PARENT时子View也只有AT_MOST(因为本View都不能确定自己的准确值,所以子View也不能确定)            } else if (childDimension == LayoutParams.WRAP_CONTENT) {                // Child wants to determine its own size. It can't be                // bigger than us.                resultSize = size;                resultMode = MeasureSpec.AT_MOST;            }            break;        // Parent asked to see how big we want to be        case MeasureSpec.UNSPECIFIED:            if (childDimension >= 0) {                // Child wants a specific size... let him have it                resultSize = childDimension;                resultMode = MeasureSpec.EXACTLY;            } else if (childDimension == LayoutParams.MATCH_PARENT) {                // Child wants to be our size... find out how big it should                // be                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;                resultMode = MeasureSpec.UNSPECIFIED;//父View对自己的大小没限制,所以也不需要限制子View的大小——子VIEW想要多大就是多大            } else if (childDimension == LayoutParams.WRAP_CONTENT) {                // Child wants to determine its own size.... find out how                // big it should be                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;                resultMode = MeasureSpec.UNSPECIFIED;//            }            break;        }        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);    }



0 0
原创粉丝点击