从XML的属性值中获取值

时间:2014-07-24 09:58:17

标签: android android-styles styleable

我有一个从Textview扩展的自定义类,现在我需要获取布局中xml的值。我试过了

public FontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setIncludeFontPadding(false);
    int style =  attrs.getAttributeIntValue(com.android.internal.R.styleable.TextAppearance_textStyle,-1);
    init(style);
}

但我无法得到com.android.internal.R.styleable它说包不存在。我想我可以从包装外面访问它。

有没有办法从xml获取样式?

styleable.TextAppearance_textStyle的值是-2001555这会改变还是我能够通过使用来获得正确的值?

int style =  attrs.getAttributeIntValue(-2001555,-1)

1 个答案:

答案 0 :(得分:0)

Tyr以这种方式获取属性值。请注意TypedArray索引是什么,如文档中所述。

public FontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    int[] attrsArray = new int[]{android.R.attr.textStyle};
    final TypedArray array = context.obtainStyledAttributes(attrs, attrsArray);
    int style = array.getInt(0, -1);  // returns 1 for bold, 2 for italic
    array.recycle();
}
相关问题