Android : How to update the selector(StateListDrawable) programmatically

后端 未结 5 2126
耶瑟儿~
耶瑟儿~ 2020-11-28 06:05

I want to update the selector for a button programmatically.

I can do this with the xml file which is given below



        
5条回答
  •  佛祖请我去吃肉
    2020-11-28 06:18

    Very late response here but in case anyone else is having problems setting a StateListDrawable programmatically. Then as with the XML files the order in which you set the states into the StateListDrawable is important.

    For example this will work as expected:

    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
    sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
    

    This won't:

    StateListDrawable sld = new StateListDrawable();
    sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
    sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
    

提交回复
热议问题