如何获取在代码中的attrs.xml中创建的枚举

Inf*_*0re 95 enums android custom-view android-custom-view attr

我创建了一个自定义视图(在此处找到),其中包含enum类型的declare-styleable属性.在xml中,我现在可以为自定义属性选择一个枚举条目.现在我想创建一个以编程方式设置此值的方法,但我无法访问枚举.

attr.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>     
Run Code Online (Sandbox Code Playgroud)

layout.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"/>
Run Code Online (Sandbox Code Playgroud)

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

小智 92

似乎没有一种从属性枚举中获取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();
}
Run Code Online (Sandbox Code Playgroud)

如果你想将值放入枚举中,你需要自己将值映射到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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Format format = Format.fromId(a.getInt(R.styleable.IconView_icon, 0))); 
Run Code Online (Sandbox Code Playgroud)

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


ste*_*etz 24

很简单,让我们向所有人展示一个示例,以说明它是多么容易:

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>
Run Code Online (Sandbox Code Playgroud)

自定义布局:

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)];
Run Code Online (Sandbox Code Playgroud)

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

  • 但这只是对符号进行了两个并行的定义。只有当定义相同时,这才有效——也就是说,它是脆弱的。OP 期望对从 XML 生成的枚举进行代码访问。看来这应该是可能的。 (4认同)

Ole*_*bul 15

让我添加一个用 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 
}
Run Code Online (Sandbox Code Playgroud)

现在获取枚举很简单:

val a: TypedArray = obtainStyledAttributes(...)
val yourEnum: YourEnum = a.getEnum(R.styleable.YourView_someAttr, YourEnum.DEFAULT)
a.recycle()
Run Code Online (Sandbox Code Playgroud)


Dav*_*mas 12

为了理智,为了理智.确保您的序数在您声明的样式中与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];
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为在这里依靠枚举序数注定会创建不可靠的代码。一件事将被更新,而不是另一件事,那么您将遇到麻烦。 (2认同)
  • 那么什么是更好的方法呢? (2认同)

小智 5

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

https://github.com/afterecho/create_enum_from_xml 上有一个小演示和自述文件,并附有解释

希望能帮助到你。