How to get an enum which is created in attrs.xml in code

后端 未结 5 1852
小蘑菇
小蘑菇 2020-12-23 20:30

I created a custom View (find it here) with an declare-styleable attribute of type enum. In xml I can now choose one of the enum entries for my custom attribute. Now I want

5条回答
  •  一个人的身影
    2020-12-23 20:33

    It's simple let's show everybody an example just to show how easy it is:

    attr.xml:

    
        
            
            
            
            
        
    
    

    Custom layout:

    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)];
    

    now use direction like any other enumeration variable.

提交回复
热议问题