问题
I develop a custom view in Android Studio. This custom view has a custom property. The possible values of this property are declared as an enum-formatted attr in a declare-stylable tag within a resources tag, all that in an xml file (see below).
Even though there are quite some questions (including answers) about accessing xml-defined enums in this forum, none of those actually helps me in solving my concrete problem.
From within the file customview_values.xml:
<declare-styleable name="CustomView">
<attr name="customViewMode" format="enum">
<enum name="CUSTOMVIEW_UNDEFINED" value="0" />
<enum name="CUSTOMVIEW_BLINKING" value="1" />
<enum name="CUSTOMVIEW_ROLLING" value="2" />
<enum name="CUSTOMVIEW_PULSING" value="3" />
<enum name="CUSTOMVIEW_NOISE" value="4" />
</attr>
</declare-styleable>
You might accept the following circumstances: Whenever an enum's value in this xml file is changed, this change shall propagate consistently to the whole application. There should be no need to have to adapt a programmatical list somewhere else in the code, whenever one changes this xml file, because this would be very error prone (and hence not in the interest of a reasonable developer).
For this reason, I need a solution, where I can get the value of each entry of the above attr list by its name, programmatically (!).
A second application of what I am looking for is testing: I want to write a test, which checks the consistence of the above-defined list, this test necessarily needs to read the respective values by their names from the xml file.
Please help me, my fellow geniuses, you're my only hope!
来源:https://stackoverflow.com/questions/57868824/get-enum-values-from-within-declare-stylable-defined-in-xml-file-programmatica