我正在编写一些自定义视图,它们共享一些同名的属性。在attrs.xml中它们各自的<declare-styleable>部分,我想对属性使用相同的名称:
<?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的format属性,但是如果这样做,我会在控制台中得到以下错误:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute 有没有一种方法可以实现这一点,也许是某种命名空间(只是猜测)?
发布于 2010-12-17 04:17:55
解决方案:只需从两个视图中提取公共属性,并将其直接添加为<resources>节点的子节点:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
<declare-styleable name="MyView1">
<attr name="myattr1" />
<attr name="myattr2" />
...
</declare-styleable>
<declare-styleable name="MyView2">
<attr name="myattr1" />
<attr name="myattr2" />
...
</declare-styleable>
</resources>发布于 2015-10-12 17:18:27
我发布这个答案是因为上面发布的解决方案在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。
发布于 2017-02-18 17:05:04
正如Priya Singhal回答的那样,Android Studio要求在其自己的样式名称中定义公共属性名称。他们不能再在根本上了。
然而,还有一些其他事情需要注意(这就是为什么我还要添加一个答案):
示例
以下是我在最近的一个项目中所做的工作,该项目有两个自定义视图,它们都具有相同的属性。只要自定义视图仍然具有属性的名称,并且不包含format,我仍然可以从代码中正常地访问它们。
<?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>简化的示例
事实上,我甚至不需要将属性放在自定义名称下。只要我为至少一个自定义视图定义了它们(给它们一个format),我就可以在任何地方使用它们(没有format)。所以这也行得通(而且看起来更干净):
<?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>然而,对于大型项目,这可能会变得混乱,在一个位置的顶部定义它们可能会更好(正如推荐的here)。
https://stackoverflow.com/questions/4434327
复制相似问题