我在写这分享一些同名属性的一些自定义视图。在各自的<申报,设置样式>
在 attrs.xml
部分我想使用相同的名称属性:
I'm writing a few custom views which share some same-named attributes. In their respective <declare-styleable>
section in attrs.xml
I'd like to use the same names for attributes:
<?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
已经定义。我发现,我应该省略格式
属性 myattr1
和 myattr2
在 MyView2
,但如果我这样做,我得到了下面的错误在控制台:
I'm getting an error saying that myattr1
and myattr2
are already defined. I found that I should omit the format
attribute for myattr1
and myattr2
in MyView2
, but if I do that, I obtain the following error in the console:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute
有没有一种方法,我可以做到这一点,也许某种命名空间(只是猜测)?
Is there a way I could accomplish this, maybe some sort of namespacing (just guessing)?
感谢您的帮助;)
推荐答案
只需提取这两种观点共同属性,并直接将它们添加为&LT儿童;资源&GT;
节点:
SOLVED!
Simply extract common attributes from both views and add them directly as children of the <resources>
node:
<?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>
相关推荐
最新文章