我有一个代码可以在单击时更改图像按钮的色调。
这是java代码
 button.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) 
        {
            if (me.getAction() == MotionEvent.ACTION_DOWN) 
            {
                button.setColorFilter(Color.argb(150, 155, 155, 155));
            } 
            else if (me.getAction() == MotionEvent.ACTION_UP) 
            {
                button.setColorFilter(Color.argb(0, 155, 155, 155)); 
                }
            return false;
        }
    });
该代码在此 xml 上运行良好,单击时按钮变暗。
  <ImageButton
    android:id="@+id/schedule"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="138dp"
    android:layout_y="169dp"
    android:src="@drawable/schedule"
    />
但它不适用于此 xml,单击时按钮不会变暗。
  <ImageButton
    android:id="@+id/schedule"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="138dp"
    android:layout_y="169dp"
    android:background="@drawable/schedule"
    />
为什么如果我使用 android:background setColorFilter 不起作用?但如果我使用 android:src 它工作正常。