7

Tôi có chế độ xem tùy chỉnh A có Chế độ xem văn bản. Tôi đã thực hiện một phương thức trả về resourceID cho TextView. Nếu không có văn bản nào được định nghĩa thì phương thức sẽ trả về -1 theo mặc định. Tôi cũng có chế độ xem tùy chỉnh B được kế thừa từ chế độ xem A. Chế độ xem tùy chỉnh của tôi có văn bản 'hello'. Khi tôi gọi phương thức để lấy thuộc tính của lớp siêu, tôi lấy -1 trở lại.Android: Cách nhận thuộc tính từ lớp siêu của chế độ xem tùy chỉnh

Trong mã cũng có một ví dụ về cách tôi có thể truy xuất giá trị nhưng nó cảm thấy loại hacky.

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> 

Class A

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); 
} 

Class 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); 
    } 
} 

Một giải pháp tôi có f ound cho đến nay là để làm như sau. Tôi cũng nghĩ rằng đây là loại hacky.

<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> 

Làm cách nào để nhận thuộc tính từ cấp độ siêu của chế độ xem tùy chỉnh? Tôi dường như không tìm thấy bất kỳ ví dụ hay nào về cách thức hoạt động của kế thừa với chế độ xem tùy chỉnh.

Trả lời

8

Rõ ràng đây là đúng cách để làm điều đó:

protected void init(Context context, AttributeSet attrs, int defStyle) { 
    super.init(context, attrs, defStyle); 

    TypedArray b = context.obtainStyledAttributes(attrs, R.styleable.B, defStyle, 0); 
    int subTextId = getSubTextId(b); 
    b.recycle(); 

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.A, defStyle, 0); 
    int mainTextId = getMainTextId(a); 
    a.recycle(); 

    if (subTextId != UNDEFINED) { 
     setSubText(subTextId); 
    } 
} 

Có một ví dụ tại nguồn của TextView.java. tại dòng 1098

Các vấn đề liên quan