我希望我的ViewA和ViewB都有「標題」標籤。但我不能把這個attrs.xml
:如何爲不同的標籤聲明幾個具有相同名稱的stylable屬性?
<?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>
因爲錯誤屬性「稱號」已被定義。 Another question示出了該解決方案:
<?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>
但是在這種情況下,不產生R.styleable.ViewA_title
和R.styleable.ViewB_title
。我需要它們使用以下代碼從AttributeSet中讀取屬性:
TypedArray a=getContext().obtainStyledAttributes(as, R.styleable.ViewA);
String title = a.getString(R.styleable.ViewA_title);
我該如何解決這個問題?
類似於http://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view – Suragch