【发布时间】:2014-07-09 10:11:56
【问题描述】:
代码:
public class CustomLayoutWithText extends LinearLayout {
private Context context;
private AttributeSet attrs;
public CustomLayoutWithText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attrs = attrs;
fooAttrs();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
fooAttrs();
}
private void fooAttrs() {
int[] set = {
android.R.attr.text // idx 0
};
TypedArray a = context.obtainStyledAttributes(attrs, set);
Log.d(null, a.getString(0));
}
}
和 XML:
<com.korovyansk.android.views.CustomLayoutWithText
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some text"/>
有理由预期输出将是:
一些文字
一些文字
但它是:
一些文字
空
为什么第二次显示为空?又该如何避免呢?
【问题讨论】:
-
转储 attrs 并查看它们是否改变
-
@pskink attrs 是一个巨大的对象,hashCode 返回相同的值,但它可能没有被覆盖。转储、toString 或其他什么方法?
-
使用 getAttributeName/getAttributeValue ?
标签: android