<declare-styleable name="ExTextView">
<attr name="enableOnPad" format="boolean"/>
<attr name="supportDeviceType" format="reference"/>
</declare-styleable>
<declare-styleable name="ExEditText">
<attr name="enableOnPad" format="boolean"/> <!-- 重複 -->
<attr name="supportDeviceType" format="reference"/> <!-- 重複 -->
</declare-styleable>
代碼冗余、維護成本高、易出現不一致。
<declare-styleable name="ExEditText" parent="ExTextView"/>
雖然語法通過,但 R.styleable.ExEditText_enableOnPad **不會生成**,導致代碼中無法引用,ADT/AAPT 已知限制。
在 <resources> 根節點直接定義 <attr>,再在各 declare-styleable 中**僅引用名稱**:
<resources>
<!-- 全局定義一次 -->
<attr name="enableOnPad" format="boolean"/>
<attr name="supportDeviceType" format="reference"/>
<declare-styleable name="ExTextView">
<attr name="enableOnPad"/>
<attr name="supportDeviceType"/>
</declare-styleable>
<declare-styleable name="ExEditText">
<attr name="enableOnPad"/>
<attr name="supportDeviceType"/>
</declare-styleable>
</resources>
R.attr.enableOnPad 唯一生成;R.styleable.ExTextView 包含 enableOnPad 索引;R.styleable.ExEditText 同樣包含,索引值**與 ExTextView 完全一致**;TypedArray.getBoolean(R.styleable.ExTextView_enableOnPad, ...) 正常工作。attrs_base.xml 集中定義所有通用屬性;attrs_xxx.xml 僅引用,不重複定義 format;StackOverflow 討論:How to declare several stylable attributes with the same name for different tags
一處定義、多處引用,是資源文件規範化的核心原則。