我一直在寻找这个,但我没有找到。我已经有一个OnTouchListener
为按钮着色的变量并且它工作得很好,但我想让它在布局上工作。
这是我的OnTouchListener
public static OnTouchListener buttonTinter = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
synchronized (lock) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
Drawable d = v.getBackground();
if (d != null) {
d.setColorFilter(Color.argb(255, 155, 155, 155),
PorterDuff.Mode.DARKEN);
}
return false;
} else if (me.getAction() == MotionEvent.ACTION_UP
|| me.getAction() == MotionEvent.ACTION_CANCEL) {
Drawable d = v.getBackground();
if (d != null) {
d.setColorFilter(Color.argb(255, 255, 255, 255),
PorterDuff.Mode.MULTIPLY);
}
if (SystemClock.elapsedRealtime() - mLastClickTime < 300) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}
return false;
}
}
};
和synchronized
时间部分只是为了避免同时触摸2个按钮的错误。因此,它在 Buttons 或 ImageButtons 中完美运行,但是当我将其设置为 RelativeLayouts 或 LinearLayouts 时没有任何反应(没有错误,它只是忽略了代码)。有人有什么建议吗?
解决方案:
布局的背景必须是可绘制的,而不是颜色