Android

http://blog.csdn.net/zjh_1110120/article/details/50976027

1.attr format 取值类型

以ShapeView 为例

<declare-styleable name="ShapeViewStyle">
        <attr name="viewWidth" format="dimension|reference"/>
        <attr name="viewHeight" format="dimension|reference"/>
        <attr name="viewColor" format="color|reference"/>

        <attr name="viewShape" format="enum">
            <enum name="rect" value="0"/>
            <enum name="oval" value="1"/>
            <enum name="line" value="2"/>
        </attr>

        <attr name="lineWidth" format="dimension|reference"/>
        <attr name="lineDashWidth" format="dimension|reference"/>
        <attr name="lineDashGap" format="dimension|reference"/>
    </declare-styleable>
<com.example.administrator.llab.view.ShapeView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:viewColor="@color/red"
                    app:viewHeight="50dp"
                    app:viewShape="oval"
                    app:viewWidth="50dp"/>

TypedArray 详解

http://blog.csdn.net/zjh_1110120/article/details/50986589

大体意思是:TypedArray 是一个数组容器,在这个容器中装由 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtainAttributes(AttributeSet, int[]) 函数获取到的属性值。用完之后记得调用 recycle() 函数回收资源。索引值用来获取 Attributes 对应的属性值(这个 Attributes 将会被传入 obtainStyledAttributes() 函数)。

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ShapeViewStyle, defStyleAttr, 0);

        viewWidth = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_viewWidth, viewWidth);
        viewHeight = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_viewHeight, viewHeight);
        viewColor = typedArray.getColor(R.styleable.ShapeViewStyle_viewColor, viewColor);

        lineWidth = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_lineWidth, lineWidth);
        lineDashWidth = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_lineDashWidth, lineDashWidth);
        lineDashGap = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_lineDashGap, lineDashGap);

        int shape = typedArray.getInt(R.styleable.ShapeViewStyle_viewShape, Shape.rect.ordinal());
        for (Shape shapeValue: Shape.values()) {
            if (shapeValue.ordinal() == shape) {
                viewShape = shapeValue;
                break;
            }
        }

        setImageDrawable(createShapeView());
    }
private LayerDrawable createShapeView() {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(viewColor);
        gradientDrawable.setSize(viewWidth, viewHeight);

        switch (viewShape) {
            case rect:
                gradientDrawable.setShape(GradientDrawable.RECTANGLE);
                break;
            case oval:
                gradientDrawable.setShape(GradientDrawable.OVAL);
                break;
            case line:
                gradientDrawable.setShape(GradientDrawable.LINE);
                gradientDrawable.setStroke(lineWidth, viewColor, lineDashWidth, lineDashGap);
                break;
        }
        return new LayerDrawable(new Drawable[]{gradientDrawable});
    }
原文地址:https://www.cnblogs.com/qlky/p/7237248.html