在Set动态图像选择器上冻结应用程序

时间:2015-11-27 06:44:18

标签: android android-layout

I need to set Selector dynamically  

我从How to set an image as a Background to a Button using StateListDrawable (dynamically or programmatically or through code)

获得了帮助

当我尝试设置默认图像时,它不起作用

state.addState(new int[] { },selectedthumb);

请帮帮我

我尝试了什么

StateListDrawable state = new StateListDrawable();
state.addState(new int[] { android.R.attr.state_selected },
                        selectedthumb);
state.addState(new int[] { android.R.attr.state_focused },  thumb);

state.addState(new int[] {  },thumb);

2 个答案:

答案 0 :(得分:1)

public void selector(Button b,int selectedthumb,int thumb)
    {
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[] {- android.R.attr.state_pressed }, getResources().getDrawable(selectedthumb));
        states.addState(new int[] {}, getResources().getDrawable(thumb));
        b.setBackgroundDrawable(states);
    }

答案 1 :(得分:0)

Check this answer 您需要使用所需状态的负值。 E.g:

states.addState(new int[] {-android.R.attr.state_enabled},R.drawable.btn_disabled);