How do you get “android:” tag values in a Custom View

前端 未结 2 2063
太阳男子
太阳男子 2020-12-24 10:48

There seems to be a lot of \"similar\" questions and answers to this scattered around which all refer to how to get a custom attribute from an AttributeSet. Wha

相关标签:
2条回答
  • 2020-12-24 11:48

    The namespace should be "http://schemas.android.com/apk/res/android" android is an alias declared in your xml file

    0 讨论(0)
  • 2020-12-24 11:50

    First declare required attributes in :

    res\attrs.xml

        <declare-styleable name="StatusThumbnail">
            <attr name="statusThumbnailattr" format="string"/>
        </declare-styleable>
    

    then in your XML layout declaration use the same attribute

    <com.custom.view.StatusThumbnail
            android:id="@+id/statusThumbnailContainer"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            statusThumbnailattr="some value"
            android:layout_weight="1"/>
    

    Access using

    public StatusThumbnail(Context context, AttributeSet attrs) {
        super(context, attrs);
    TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.StatusThumbnail);
    this.mdColorDialogTitle=a.getString(R.styleable.StatusThumbnail_statusThumbnailattr);
    }
    
    0 讨论(0)
提交回复
热议问题