gpt4 book ai didi

java - 从代码访问XML枚举值

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:24 25 4
gpt4 key购买 nike

我用枚举值声明了一个可样式化的属性,如下所示:

<declare-styleable name="TileLayout">
<attr name="rotation" format="integer">
<enum name="top" value="0"/>
<enum name="left" value="1"/>
<enum name="right" value="2"/>
<enum name="bottom" value="3"/>
</attr>
</declare-styleable>
现在,我想在我的代码中引用它们,最好在 switch语句中引用它们。
除了将枚举中的值硬编码到我的代码中(首先击败枚举目的的一半)之外,我找不到其他方法。
有谁知道如何做到这一点?
编辑
根据@CommonsWare的回答,尝试了以下操作:
resources.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="rotation_top">0</integer>
</resources>
attrs.xml:
<resources>
<declare-styleable name="TileLayout">
<attr name="rotation" format="integer">
<enum name="top" value="@integer/rotation_top"/>
<enum name="left" value="1"/>
<enum name="right" value="2"/>
<enum name="bottom" value="3"/>
</attr>
</declare-styleable>
</resources>
这导致 top不再是 rotation的可接受值。

最佳答案

您可以尝试将值定义为integer resources并引用其他资源(您的attrs.xml和Java)。

但是,在IIRC中,如果不使用Java对其进行硬编码,就无法使用switch语句,因为switch语句需要case子句的常量值。

关于java - 从代码访问XML枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com