Accessing XML Enumeration Values โ€‹โ€‹from Code

I have declared a styleable attribute with enum values โ€‹โ€‹like:

<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>

      

Now I would like to refer to those contained in my code, preferably in an expression switch

.

I can't seem to find a way to do this other than just hard-coding the values โ€‹โ€‹I have in the enum into my code (defeating half of the purpose of the enum in the first place).

Does anyone know how to do this?

EDIT

In response to @CommonsWare, tried this:

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>

      

This resulted in what was top

no longer an accepted meaning for rotation

.

+3


source to share


1 answer


You can try defining the values โ€‹โ€‹as whole resources and referring to the resources elsewhere (yours attrs.xml

and Java).



However, the IIRC operator, operator is switch

not possible without hard-coding in Java, since the operator switch

requires constant values โ€‹โ€‹for sentences case

.

+2


source







All Articles