以编程方式获取styleable属性的名称

3
有没有一种编程方式可以获取styleable属性的名称?

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

例如,我想从R.styleable.TextView_Text整数值中获取“Text”。
1个回答

2

您的Styleable是:

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

使用以下代码在任何位置调用:

custom:DataContext="@string/xxxx"

在你的代码中:
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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接