在attrs.xml自定义视图同名属性 [英] Same-named attributes in attrs.xml for custom view

查看:410
本文介绍了在attrs.xml自定义视图同名属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这分享一些同名属性的一些自定义视图。在各自的<申报,设置样式> 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>

这篇关于在attrs.xml自定义视图同名属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆