【问题标题】:Custom Attributes in AndroidAndroid 中的自定义属性
【发布时间】:2010-12-31 06:53:09
【问题描述】:

我正在尝试为所有可编辑元素创建一个名为 Tag 的自定义属性。我将以下内容添加到 attrs.xml

<declare-styleable name="Spinner">
    <attr name="tag" format="string" />
</declare-styleable>

<declare-styleable name="EditText">
    <attr name="tag" format="string" />
</declare-styleable>

我收到一条错误消息,提示“已为 EditText 定义属性标签”。不能在不同的元素上创建同名的自定义属性吗?

【问题讨论】:

    标签: android custom-attributes


    【解决方案1】:

    如果您要在多个地方使用 attr,请将其放在 &lt;resources&gt; 元素内的根元素中,如下所示:

    <resources>
    
        <attr name="tag" format="string" />
    
        <declare-styleable name="Spinner">
            <attr name="tag" />
        </declare-styleable>
    
        <declare-styleable name="EditText">
            <attr name="tag" />
        </declare-styleable>
    
    </resources>
    

    现在您可以在此 xml 文件中的任何位置使用 tag 属性。

    希望有帮助。

    【讨论】:

      【解决方案2】:

      看看我关于自定义属性的详细回答是否有帮助:Defining custom attrs

      【讨论】:

        猜你喜欢
        • 2012-01-28
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-12
        • 2013-09-01
        • 1970-01-01
        相关资源
        最近更新 更多