Android - 自訂控制項和屬性(attr和TypedArray)

來源:互聯網
上載者:User

標籤:cycle   contex   values   format   數組   return   取值   csdn   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});    }

 

Android - 自訂控制項和屬性(attr和TypedArray)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.