I need to set Selector dynamically
获得了帮助
当我尝试设置默认图像时,它不起作用
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);
答案 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);