我希望我的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