代码实现 按钮点击出现不同的图片或者颜色
不仅仅是button可以,像TextView也是可以实现的。
改变button的颜色
StateListDrawable stateListDrawable = new StateListDrawable();
ColorDrawable colorDrawableNormal = new ColorDrawable();
ColorDrawable colorDrawablePressed = new ColorDrawable();
colorDrawableNormal.setColor(Color.BLUE);
colorDrawablePressed.setColor(Color.RED);
//按下状态
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, colorDrawablePressed);
//正常状态
stateListDrawable.addState(new int[]{}, colorDrawableNormal);
btn1.setBackground(stateListDrawable);
改变button的背景
StateListDrawable stateListDrawable = new StateListDrawable();
Drawable drawablePressed = getResources().getDrawable(R.mipmap.ic_action_add);
//按下状态
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);
//正常状态
stateListDrawable.addState(new int[]{}, colorDrawableNormal);
btn1.setBackground(stateListDrawable);
本文介绍如何使用StateListDrawable实现按钮点击时颜色或背景图片的变化效果,包括设置不同状态下的颜色和图片资源。
908

被折叠的 条评论
为什么被折叠?



