如何从“本地视图”取回属性?

我展开AppBarLayout并在XML布局中使用以下属性:

app:expanded="false"


我知道如何从自定义样式元素获取属性,但是如何从标准属性获取属性呢?

TypedArray ta = context.obtainStyledAttributes(attrs, ???? );
expanded = ta.getBoolean( ???? , false);


谢谢你的帮助。

最佳答案

我找到了一种方法,但是我不确定这是一个好方法。

我定义了一个样式元素并重新定义了相同的名称:

<declare-styleable name="attrs_global_boolean">
    <attr name="expanded" format="boolean"/>
    ....
</declare-styleable>

<declare-styleable name="AppBarLayout"> // here i use the same name of the original class, doesn't seem to give me any error, maybe it is better do use AppBarLayoutCustom or AppBarLayoutExtra (or any other name ?? )
    <attr name="expanded"/>
</declare-styleable>


然后

final static protected int[] STYLEABLE_AppBarLayout = R.styleable.AppBarLayout;
TypedArray ta = context.obtainStyledAttributes(attrs, STYLEABLE_AppBarLayout );
expanded = ta.getBoolean( R.styleable.AppBarLayout_expanded, false);
ta.recycle();


有没有需要重新定义具有相同属性名称的样式的方法呢?更好的方法呢?

谢谢

09-25 21:47