124

我创建了一个自定义视图(在此处找到),它具有 enum 类型的可声明样式属性。在 xml 中,我现在可以为我的自定义属性选择枚举条目之一。现在我想创建一个以编程方式设置此值的方法,但我无法访问枚举。

属性.xml

<declare-styleable name="IconView">
    <attr name="icon" format="enum">
        <enum name="enum_name_one" value="0"/>
        ....
        <enum name="enum_name_n" value="666"/>
   </attr>
</declare-styleable>     

布局.xml

<com.xyz.views.IconView
    android:id="@+id/heart_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:icon="enum_name_x"/>

我需要的是这样的:mCustomView.setIcon(R.id.enum_name_x); 但我找不到枚举,或者我什至不知道如何获得枚举或枚举的名称。

4

5 回答 5

105

似乎没有一种从属性枚举中获取 Java 枚举的自动方法——在 Java 中,您可以获得指定的数值——该字符串用于 XML 文件(如您所示)。

您可以在视图构造函数中执行此操作:

TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.IconView,
                0, 0);

    // Gets you the 'value' number - 0 or 666 in your example
    if (a.hasValue(R.styleable.IconView_icon)) {
        int value = a.getInt(R.styleable.IconView_icon, 0));
    }

    a.recycle();
}

如果要将值转换为枚举,则需要自己将值映射到 Java 枚举,例如:

private enum Format {
    enum_name_one(0), enum_name_n(666);
    int id;

    Format(int id) {
        this.id = id;
    }

    static Format fromId(int id) {
        for (Format f : values()) {
            if (f.id == id) return f;
        }
        throw new IllegalArgumentException();
    }
}

然后在第一个代码块中,您可以使用:

Format format = Format.fromId(a.getInt(R.styleable.IconView_icon, 0))); 

(虽然此时抛出异常可能不是一个好主意,可能最好选择一个合理的默认值)

于 2013-09-24T20:58:14.667 回答
52

很简单,让我们给大家看一个例子来说明它是多么容易:

attr.xml:

<declare-styleable name="MyMotionLayout">
    <attr name="motionOrientation" format="enum">
        <enum name="RIGHT_TO_LEFT" value="0"/>
        <enum name="LEFT_TO_RIGHT" value="1"/>
        <enum name="TOP_TO_BOTTOM" value="2"/>
        <enum name="BOTTOM_TO_TOP" value="3"/>
    </attr>
</declare-styleable>

自定义布局:

public enum Direction {RIGHT_TO_LEFT, LEFT_TO_RIGHT, TOP_TO_BOTTOM, BOTTOM_TO_TOP}
Direction direction;
...
    TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyMotionLayout);
    Direction direction = Direction.values()[ta.getInt(R.styleable.MyMotionLayout_motionOrientation,0)];

现在像任何其他枚举变量一样使用方向。

于 2019-01-24T07:51:18.397 回答
17

让我添加一个用 kotlin 编写的解决方案。添加内联扩展功能:

inline fun <reified T : Enum<T>> TypedArray.getEnum(index: Int, default: T) =
    getInt(index, -1).let { if (it >= 0) enumValues<T>()[it] else default 
}

现在获取枚举很简单:

val a: TypedArray = obtainStyledAttributes(...)
val yourEnum: YourEnum = a.getEnum(R.styleable.YourView_someAttr, YourEnum.DEFAULT)
a.recycle()
于 2019-11-12T09:26:57.353 回答
13

好吧,为了理智。确保您声明的样式中的序号与 Enum 声明中的序号相同,并将其作为数组访问。

TypedArray a = context.getTheme().obtainStyledAttributes(
                   attrs,
                   R.styleable.IconView,
                   0, 0);

int ordinal = a.getInt(R.styleable.IconView_icon, 0);

if (ordinal >= 0 && ordinal < MyEnum.values().length) {
      enumValue = MyEnum.values()[ordinal];
}
于 2015-03-04T21:52:02.110 回答
5

我知道问题发布已经有一段时间了,但我最近遇到了同样的问题。我一起破解了一些东西,使用 Square 的 JavaPoet 和 build.gradle 中的一些东西,在项目构建时自动从 attrs.xml 创建一个 Java 枚举类。

在https://github.com/afterecho/create_enum_from_xml有一个小演示和一个带有解释的自述文件

希望能帮助到你。

于 2015-06-10T21:56:52.960 回答