attrs.xml中創建的枚舉我使用enum類型的declare-styleable屬性創建了自定義View(查找它here)。在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>
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"/>
我需要的是這樣的:mCustomView.setIcon(R.id.enum_name_x);
但我找不到枚舉或我甚至不知道我如何獲得枚舉或枚舉的名稱。
感謝
我認爲在這裏依賴枚舉序列註定要創建不可靠的代碼。有一件事會得到更新,而不是其他的,然後你會遇到麻煩。 – tir38