我正在写一些自定义视图,它们共享一些同名的属性.在他们各自的<declare-styleable>部分,attrs.xml我想对属性使用相同的名称:
<?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>
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,说明myattr1并且myattr2已经定义了.我发现我应该省略formatfor myattr1和myattr2in 的属性MyView2,但如果我这样做,我在控制台中获得以下错误:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute
Run Code Online (Sandbox Code Playgroud)
有没有办法可以实现这一点,也许某种命名空间(只是猜测)?
android android-custom-view android-view android-custom-attributes
关于declare-styleable标记的珍贵文档很少,我们可以通过它来声明组件的自定义样式.我确实找到了这个标签format属性的有效值列表attr.虽然这很好,但它没有解释如何使用其中的一些值.浏览attr.xml(标准属性的Android源代码),我发现你可以做以下事情:
<!-- The most prominent text color. -->
<attr name="textColorPrimary" format="reference|color" />
Run Code Online (Sandbox Code Playgroud)
该format属性显然可以设置为值的组合.据推测,该format属性有助于解析器解释实际的样式值.然后我在attr.xml中发现了这个:
<!-- Default text typeface. -->
<attr name="typeface">
<enum name="normal" value="0" />
<enum name="sans" value="1" />
<enum name="serif" value="2" />
<enum name="monospace" value="3" />
</attr>
<!-- Default text typeface style. -->
<attr name="textStyle">
<flag name="normal" value="0" />
<flag name="bold" value="1" />
<flag name="italic" value="2" />
</attr>
Run Code Online (Sandbox Code Playgroud)
这两个似乎都声明了一组指定样式的允许值.
所以我有两个问题:
enum值之一的样式属性与可以采用一组值的样式属性之间的区别是什么flag? …