我正在编写一些共享一些同名属性的自定义视图。在attrs.xml中各自的<declare- stylesable >节中,我想为属性使用相同的名称:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView1">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
...
</declare-styleable>
<declare-styleable name="MyView2">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
...
</declare-styleable>
</resources>
我得到一个错误说myattr1和myattr2已经被定义了。我发现我应该在MyView2中省略myattr1和myattr2的格式属性,但如果我这样做,我在控制台上获得以下错误:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute
是否有一种方法可以实现这一点,也许是某种命名空间(只是猜测)?
我发布这个答案是因为上面发布的解决方案在Android Studio中不适合我。我需要在我的自定义视图之间共享我的自定义属性,所以我在Android Studio中尝试了上述解决方案,但没有运气。所以我尝试着去做。希望它能帮助寻找同样问题的人。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- parent styleable -->
<declare-styleable name="MyView">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
</declare-styleable>
<!-- inheriting parent styleable -->
<!-- also note "myBackgroundColor" belongs to child styleable"MyView1"-->
<declare-styleable name="MyView1" parent="MyView">
<attr name="myattr1" />
<attr name="myattr2" />
<attr name="myBackgroundColor" format="color"/>
</declare-styleable>
<!-- inheriting parent styleable -->
<!-- same way here "myfonnt" belongs to child styelable "MyView2" -->
<declare-styleable name="MyView2" parent="MyView">
<attr name="myattr1" />
<attr name="myattr2" />
<attr name="myfont" format="string"/>
...
</declare-styleable>
</resources>
这对我来说完全有效。
我们需要让父对象具有可样式,然后我们需要继承父对象的可样式。例如,正如我上面所做的:
父样式名称MyView并将其分别继承到我的其他样式比如MyView1和MyView2。
正如Priya Singhal所回答的,Android Studio要求公共属性名称在自己的样式名称中定义。他们不可能再在根源了。
然而,还有一些其他的事情需要注意(这就是为什么我也添加了一个答案):
通用样式不需要与视图同名。(感谢这个回答指出了这一点。)
你不需要对父对象使用继承。
例子
以下是我在最近的一个项目中所做的,该项目有两个共享相同属性的自定义视图。只要自定义视图仍然具有属性的名称并且不包括格式,我仍然可以从代码中正常访问它们。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- common attributes to all custom text based views -->
<declare-styleable name="TextAttributes">
<attr name="text" format="string"/>
<attr name="textSize" format="dimension"/>
<attr name="textColor" format="color"/>
<attr name="gravity">
<flag name="top" value="48" />
<flag name="center" value="17" />
<flag name="bottom" value="80" />
</attr>
</declare-styleable>
<!-- custom text views -->
<declare-styleable name="View1">
<attr name="text"/>
<attr name="textSize"/>
<attr name="textColor"/>
<attr name="gravity"/>
</declare-styleable>
<declare-styleable name="View2">
<attr name="text"/>
<attr name="textSize"/>
<attr name="textColor"/>
<attr name="gravity"/>
</declare-styleable>
</resources>
流线型的例子
事实上,我甚至不需要将属性放在自定义名称下。只要我为至少一个自定义视图定义它们(给它们一个格式),我就可以在任何地方使用它们(没有格式)。所以这也是可行的(看起来更干净):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="View1">
<attr name="text" format="string"/>
<attr name="textSize" format="dimension"/>
<attr name="textColor" format="color"/>
<attr name="gravity">
<flag name="top" value="48" />
<flag name="center" value="17" />
<flag name="bottom" value="80" />
</attr>
</declare-styleable>
<declare-styleable name="View2">
<attr name="text"/>
<attr name="textSize"/>
<attr name="textColor"/>
<attr name="gravity"/>
</declare-styleable>
</resources>
然而,对于一个大型项目,这可能会很混乱,在一个单一的位置在顶部定义它们可能会更好(正如这里推荐的那样)。