Android StateListDrawable的坑
有问题的代码:
StateListDrawable background = new StateListDrawable();
CircleDrawable pressedDrawable = new CircleDrawable(PluginRely.getColor(R.color.button_pressed));
CircleDrawable drawable = new CircleDrawable(PluginRely.getColor(R.color.button_unpressed));
background.addState(new int[]{android.R.attr.state_pressed},pressedDrawable);
background.addState(new int[]{}, drawable);
mIvPlay.setBackgroundDrawable(background);
mIvPlayStop.setBackgroundDrawable(background);
没问题的代码:
StateListDrawable background = new StateListDrawable();
StateListDrawable background2 = new StateListDrawable();
CircleDrawable pressedDrawable = new CircleDrawable(PluginRely.getColor(R.color.button_pressed));
CircleDrawable drawable = new CircleDrawable(PluginRely.getColor(R.color.button_unpressed));
background.addState(new int[]{android.R.attr.state_pressed},pressedDrawable);
background.addState(new int[]{}, drawable);
background2.addState(new int[]{android.R.attr.state_pressed},pressedDrawable);
background2.addState(new int[]{}, drawable);
mIvPlay.setBackgroundDrawable(background);
mIvPlayStop.setBackgroundDrawable(background2);
有问题的代码,会导致,mIvPlay mIvPlayStop 两个ImageView 的点击状态混乱,明明不是按下态,却显示的是按下态的图片。所以,不要省事。两个ImageView 在代码里面,不可以公用同一个StateListDrawable 对象!
泰山崩于前而色不变的曹新雨,抱歉
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步