【问题标题】:Freeze application on Set dynamic image selector在设置动态图像选择器上冻结应用程序
【发布时间】:2015-11-27 06:44:18
【问题描述】:
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);

【问题讨论】:

  • 你选择的拇指是什么?
  • @ChiragSavsani Drawable

标签: android android-layout


【解决方案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);
    }

【讨论】:

    【解决方案2】:

    Check this answer 你需要使用所需状态的负值。例如:

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

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多