在Android的自定义属性自定义、属性、Android

2023-09-05 05:20:54 作者:无他无爱ヾ

我试图创建一个名为标记为可编辑的所有元素的自定义属性。我增加了以下到attrs.xml

I'm trying to create a custom attribute called Tag for all editable elements. I added the following to 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上。难道不能够在不同的元素创建一个同名的自定义属性?

I get an error saying "Attribute tag has already been defined" for the EditText. Is it not possible to create a custom attribute of the same name on different elements?

推荐答案

如果您要使用生成的attr在多个地方,然后把它的根元素的&LT内部,资源和GT; 像下面的元素:

If you are going to use an attr in more than one place then put it in the root element inside the <resources> element like the following :

<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文件。

Now you can use the tag attribute in anywhere you want inside this xml file .

希望有所帮助。