TextView가있는 사용자 정의보기 A
이 있습니다. TextView에 resourceID
을 반환하는 메서드를 만들었습니다. 텍스트가 정의되어 있지 않은 경우 메서드는 기본적으로 -1을 반환합니다. 보기 A
에서 상속받은 사용자 정의보기 B
도 있습니다. 내 사용자 정의보기에는 'hello'텍스트가 있습니다. 수퍼 클래스의 속성을 가져 오는 메서드를 호출하면 대신 -1이 반환됩니다.Android : 사용자 정의보기의 수퍼 클래스에서 속성을 얻는 방법
코드에는 값을 검색하는 방법에 대한 예제도 있지만 해커의 종류를 느낍니다.
attrs.xml이
<declare-styleable name="A">
<attr name="mainText" format="reference" />
</declare-styleable>
<declare-styleable name="B" parent="A">
<attr name="subText" format="reference" />
</declare-styleable>
급
protected static final int UNDEFINED = -1;
protected void init(Context context, AttributeSet attrs, int defStyle)
{
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.A, defStyle, 0);
int mainTextId = getMainTextId(a);
a.recycle();
if (mainTextId != UNDEFINED)
{
setMainText(mainTextId);
}
}
protected int getMainTextId(TypedArray a)
{
return a.getResourceId(R.styleable.A_mainText, UNDEFINED);
}
클래스 B
protected void init(Context context, AttributeSet attrs, int defStyle)
{
super.init(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.B, defStyle, 0);
int mainTextId = getMainTextId(a); // this returns -1 (UNDEFINED)
//this will return the value but feels kind of hacky
//TypedArray b = context.obtainStyledAttributes(attrs, R.styleable.A, defStyle, 0);
//int mainTextId = getMainTextId(b);
int subTextId = getSubTextId(a);
a.recycle();
if (subTextId != UNDEFINED)
{
setSubText(subTextId);
}
}
I는 F가 다른 솔루션 지금까지 ound는 다음을 수행하는 것입니다. 나는 이것이 일종의 해커라고 생각한다.
<attr name="mainText" format="reference" />
<declare-styleable name="A">
<attr name="mainText" />
</declare-styleable>
<declare-styleable name="B" parent="A">
<attr name="mainText" />
<attr name="subText" format="reference" />
</declare-styleable>
사용자 정의보기의 수퍼 클래스에서 속성을 가져 오는 방법은 무엇입니까? 상속이 사용자 지정보기에서 작동하는 방식에 대한 좋은 예를 찾을 수없는 것 같습니다.