当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Get name of styleable attribute programmatically

问题描述:

Is there a way to programmatically get name of styleable attribute:

<declare-styleable name="TextView">

<attr name="DataContext" format="string" />

<attr name="Text" format="string" />

</declare-styleable>

For example I want to get "Text" from R.styleable.TextView_Text integer value.

网友答案:

Your Styleable is:

<declare-styleable name="TextView">
    <attr name="DataContext" format="string" />
    <attr name="Text" format="string" />
</declare-styleable>

Call it where you want with this:

custom:DataContext="@string/xxxx"

In your Code:

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextView);


  int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        int attr = a.getIndex(i);
        switch (attr) {
        case R.styleable.TextView_DataContext:
            title = a.getString(attr);
            break;

        case R.styleable.TextVie_Text:
            //Any you want
            break;
        default:
            Log.d("TAG", "Unknown attribute for " + getClass().toString() + ": " + attr);
            break;
        }
    }

    a.recycle();
分享给朋友:
您可能感兴趣的文章:
随机阅读: