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_title
và R.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?
Tương tự như http://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view – Suragch