首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义视图的attrs.xml中的同名属性

自定义视图的attrs.xml中的同名属性
EN

Stack Overflow用户
提问于 2010-12-14 06:58:41
回答 5查看 43K关注 0票数 211

我正在编写一些自定义视图,它们共享一些同名的属性。在attrs.xml中它们各自的<declare-styleable>部分,我想对属性使用相同的名称:

代码语言:javascript
复制
<?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>

我收到一个错误,说myattr1myattr2已经定义好了。我发现我应该在MyView2中省略myattr1myattr2format属性,但是如果这样做,我会在控制台中得到以下错误:

代码语言:javascript
复制
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute 

有没有一种方法可以实现这一点,也许是某种命名空间(只是猜测)?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-12-17 04:17:55

解决方案:只需从两个视图中提取公共属性,并将其直接添加为<resources>节点的子节点:

代码语言:javascript
复制
<?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>
票数 442
EN

Stack Overflow用户

发布于 2015-10-12 17:18:27

我发布这个答案是因为上面发布的解决方案在Android Studio中对我不起作用。我需要在我的自定义视图之间共享我的自定义属性,所以我在Android Studio中尝试了上面的解决方案,但没有成功。因此,我进行了实验,并尝试了一种方法。希望它能帮助那些正在寻找同样问题的人。

代码语言:javascript
复制
  <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <!-- parent styleable -->
     <declare-styleable name="MyView">
         <attr name="myattr1" format="string" />
         <attr name="myattr2" format="dimension" />
     </declare-styleable>

     <!-- inheriting parent styleable -->
     <!-- also note "myBackgroundColor" belongs to child styleable"MyView1"-->
    <declare-styleable name="MyView1" parent="MyView">
        <attr name="myattr1" />
        <attr name="myattr2" />
        <attr name="myBackgroundColor" format="color"/>
    </declare-styleable>


    <!-- inheriting parent styleable -->
    <!-- same way here "myfonnt" belongs to child styelable "MyView2" -->
    <declare-styleable name="MyView2" parent="MyView">
        <attr name="myattr1" />
        <attr name="myattr2" />
        <attr name="myfont" format="string"/>
        ...
    </declare-styleable>
</resources>

这对我来说完全有效。我们需要让一个父元素成为可样式化的,然后我们需要继承这个父元素的样式。例如,正如我上面所做的:父样式名称MyView,并将其分别继承到我的其他样式名称,如MyView1和MyView2。

票数 65
EN

Stack Overflow用户

发布于 2017-02-18 17:05:04

正如Priya Singhal回答的那样,Android Studio要求在其自己的样式名称中定义公共属性名称。他们不能再在根本上了。

然而,还有一些其他事情需要注意(这就是为什么我还要添加一个答案):

  • 常用样式不需要与视图命名相同。(感谢this answer指出这一点。)
  • 你不需要对父级使用继承。

示例

以下是我在最近的一个项目中所做的工作,该项目有两个自定义视图,它们都具有相同的属性。只要自定义视图仍然具有属性的名称,并且不包含format,我仍然可以从代码中正常地访问它们。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- common attributes to all custom text based views -->

    <declare-styleable name="TextAttributes">
        <attr name="text" format="string"/>
        <attr name="textSize" format="dimension"/>
        <attr name="textColor" format="color"/>
        <attr name="gravity">
            <flag name="top" value="48" />
            <flag name="center" value="17" />
            <flag name="bottom" value="80" />
        </attr>
    </declare-styleable>

    <!-- custom text views -->

    <declare-styleable name="View1">
        <attr name="text"/>
        <attr name="textSize"/>
        <attr name="textColor"/>
        <attr name="gravity"/>
    </declare-styleable>

    <declare-styleable name="View2">
        <attr name="text"/>
        <attr name="textSize"/>
        <attr name="textColor"/>
        <attr name="gravity"/>
    </declare-styleable>

</resources>

简化的示例

事实上,我甚至不需要将属性放在自定义名称下。只要我为至少一个自定义视图定义了它们(给它们一个format),我就可以在任何地方使用它们(没有format)。所以这也行得通(而且看起来更干净):

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="View1">
        <attr name="text" format="string"/>
        <attr name="textSize" format="dimension"/>
        <attr name="textColor" format="color"/>
        <attr name="gravity">
            <flag name="top" value="48" />
            <flag name="center" value="17" />
            <flag name="bottom" value="80" />
        </attr>
    </declare-styleable>

    <declare-styleable name="View2">
        <attr name="text"/>
        <attr name="textSize"/>
        <attr name="textColor"/>
        <attr name="gravity"/>
    </declare-styleable>

</resources>

然而,对于大型项目,这可能会变得混乱,在一个位置的顶部定义它们可能会更好(正如推荐的here)。

票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4434327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档