코드 :매직
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"/>
가 출력 될 것입니다 :
일부 텍스트
일부 텍스트
그러나 그것 :
일부 텍스트
null
두 번째로 null로 표시되는 이유는 무엇입니까? 어떻게 피하는가?
attrs를 덤프하여 변경되는지 확인 – pskink
@pskink attrs는 거대한 개체이므로 hashCode는 같은 값을 반환하지만 무시되지 않을 수 있습니다. 덤프, toString 또는 다른 어떤 메소드에 사용할 메소드는 무엇입니까? – AlexKorovyansky
getAttributeName/getAttributeValue를 사용 하시겠습니까? – pskink