自定义视图的 attrs.xml 中的同名属性
我正在编写一些共享一些同名属性的自定义视图。在 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>
我收到一条错误消息,指出 myattr1
和 myattr2
已定义。我发现我应该在 MyView2
中省略 myattr1
和 myattr2
的 format
属性,但如果我这样做,我在控制台中收到以下错误:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute
有没有办法可以完成此操作,也许是某种命名空间(只是猜测)?
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>
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案:只需从两个视图中提取公共属性并将它们直接添加为
节点的子节点:Solution: Simply extract common attributes from both views and add them directly as children of the
<resources>
node:我发布这个答案是因为上面发布的解决方案在 Android Studio 中不适合我。我需要在自定义视图之间共享自定义属性,因此我在 Android Studio 中尝试了上述解决方案,但没有成功。所以我尝试并想办法去做。希望它可以帮助正在寻找同样问题的人。
这对我来说完全有效。
我们需要使父级可样式化,然后我们需要继承该父级可样式化。例如,正如我上面所做的:
父样式名称为 MyView 并将其继承到我的其他样式,例如 MyView1 和 MyView2 。
I am posting this answer as the above-posted solution didn't work out for me in Android Studio. I need to share my custom attributes among my custom views so I tried the above solution in Android Studio but had no luck. So I experiment and go a way to do it. Hope it might help someone looking for the same problem.
This works for me completely.
We need to make a Parent styleable and then we need to inherit that parent styleable. For example, as I have done above :
Parent styleable name MyView and inherited this to my other styleable like MyView1 and MyView2 respectively.
正如 Priya Singhal 回答的那样,Android Studio 要求在自己的样式名称中定义公共属性名称。他们不能再是根源了。
但是,还有其他一些事情需要注意(这就是为什么我还添加答案):
示例
这是我在最近的一个项目中所做的,该项目有两个共享相同属性的自定义视图。只要自定义视图仍然具有属性名称并且不包含
格式
,我仍然可以通过代码正常访问它们。简化的示例
事实上,我什至不需要将属性放在自定义名称下。只要我为至少一个自定义视图定义它们(给它们一个
格式
),我就可以在任何地方使用它们(没有格式
)。所以这也有效(并且看起来更干净):但是,对于大型项目,这可能会变得混乱,并且在单个位置的顶部定义它们可能会更好(建议此处)。
As Priya Singhal answered, Android Studio requires the common attribute names to be defined within their own style name. They can't be at the root any more.
However, there are a couple other things to note (which is why I am also adding an answer):
Example
Here is what I did in a recent project that has two custom views that both share the same attributes. As long as the custom views still have the names for the attributes and don't include a
format
, I can still access them as normal from code.Streamlined example
In fact, I don't even need to put the attributes under a custom name. As long as I define them (give them a
format
) for at least one custom view, I can use them anywhere (without theformat
). So this also works (and looks cleaner):For a large project, though, this could get messy and defining them at the top in a single location might be better (as recommended here).
谢谢刘易斯
我遇到了同样的问题,您的继承解决方案给了我像下面这样执行的提示,并且工作正常。我只是在上面声明了公共属性,然后在样式声明的主体中再次重写它,而不进行格式化。
我希望它能帮助某人
Thanks Lewis
I had the same problem , and your inheritance solution gave me the hint for doing it like below and it works fine.I just declared common attributes at the above and rewrite it in the body of style declaration again without formatting.
I hope it helps someone
以防万一有人在尝试可用的解决方案后仍然遇到这个问题。我坚持使用
string
格式添加subtitle
属性。我的解决方案是删除格式。
之前:
之后:
Just in case someone still stuck with this problem after tried available solution. I stuck with add
subtitle
attribute withstring
format.My solution is remove the format.
before:
<attr name="subtitle" format="string"/>
after:
<attr name="subtitle"/>