2013-09-16 15 views
17

Tôi muốn cả ViewA và ViewB của mình có thẻ "tiêu đề". Nhưng tôi không thể đặt điều này trong attrs.xml:Làm cách nào để khai báo một số thuộc tính có thể tạo kiểu với cùng tên cho các thẻ khác nhau?

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 
    <declare-styleable name="ViewB"> 
     <attr name="title" format="string" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

vì lỗi Thuộc tính "title" đã được xác định. Another question cho thấy giải pháp này:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="title" format="string" /> 
    <declare-styleable name="ViewB"> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

nhưng trong trường hợp đó, R.styleable.ViewA_titleR.styleable.ViewB_title không được tạo ra. Tôi cần chúng để đọc các thuộc tính từ AttributeSet bằng cách sử dụng mã sau:

TypedArray a=getContext().obtainStyledAttributes(as, R.styleable.ViewA); 
String title = a.getString(R.styleable.ViewA_title); 

Làm cách nào để giải quyết vấn đề này?

+0

Tương tự như http://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view – Suragch

Trả lời

32

Liên kết bạn đã gửi không cung cấp cho bạn câu trả lời chính xác. Đây là những gì nó cho thấy bạn làm:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="title" format="string" /> 
    <declare-styleable name="ViewA"> 
     <attr name="title" /> 
    </declare-styleable> 
    <declare-styleable name="ViewB"> 
     <attr name="title" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

Bây giờ, R.styleable.ViewA_titleR.styleable.ViewB_title đều dễ tiếp cận.

Nếu bạn có cơ hội, hãy đọc qua câu trả lời này: Link. Trích dẫn có liên quan:

Bạn có thể xác định thuộc tính ở phần tử trên cùng hoặc bên trong của một phần tử. Nếu tôi sẽ sử dụng một attr trong nhiều hơn hơn một nơi tôi đặt nó trong phần tử gốc.

+0

Điều này dường như không còn đúng nữa (hoặc ít nhất là khi sử dụng Android Studio 2.3).Khi tôi xác định các thuộc tính trong thư mục gốc, tôi gặp lỗi khi nói rằng các tài nguyên đó không thể được giải quyết. Tôi nghĩ rằng bạn cần phải xác định một phụ huynh với bất kỳ thuộc tính được chia sẻ. – BioeJD

+0

Nó hiện không hoạt động trên Android Studio. – user3269713

+0

câu trả lời đúng kể từ 2018-2 là https://stackoverflow.com/a/43635002/1963973 – marianosimone

3

Bạn cần phải sử dụng thừa kế

<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 

    <declare-styleable name="ViewB" parent="ViewA"> // inherit from ViewA 
     <attr name="min" format="integer" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

Trong mã java của bạn

String namespace = "http://schemas.android.com/apk/res/" + getContext().getPackageName(); 
int title_resource = attrs.getAttributeResourceValue(namespace, "title", 0); 
String title = ""; 
if(title_resource!=0){ 
    title = getContext().getString(title_resource); 
} 

int min = attrs.getAttributeResourceValue(namespace, "min", 0); // read int value 
int max = attrs.getAttributeResourceValue(namespace, "max", 0); 
+0

Điều này không hiệu quả. Nó không tạo ra ViewB_title. ViewA_title không thể được sử dụng cho các phần tử ViewB vì ID của nó sẽ xung đột với ViewB_min. – Andreas

2

Thực hiện việc này thay thế. Không parent thẻ cần

<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 

    <declare-styleable name="ViewB" > 
     <attr name="title" /> 
     <attr name="min" format="integer" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

này bởi vì một khi title được khai báo trong ViewA, nó không cần (và cũng có thể không) được khai báo trong một lần nữa trong một declare-styleable

1
<resources> 
<declare-styleable name="ViewA"> 
    <attr name="title" format="string" /> 
</declare-styleable> 

<declare-styleable name="ViewB" parent="ViewA"> // inherit from ViewA 
    <attr name="min" format="integer" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

Điều này không hoạt động。

<resources> 
<attr name="title" format="string" /> 
<declare-styleable name="ViewA"> 
    <attr name="title" /> 
</declare-styleable> 
<declare-styleable name="ViewB"> 
    <attr name="title" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

này cũng không hoạt động.

<resources> 
<declare-styleable name="ViewA"> 
    <attr name="title" format="string" /> 
</declare-styleable> 

<declare-styleable name="ViewB" > 
    <attr name="title" /> 
    <attr name="min" format="integer" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

Đây là OK !!

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