我有一个按钮,里面有一个图像。当按下按钮时,我想将图像色调的颜色更改为灰色,当再次按下按钮时,我想删除灰色。
XML:
<Button
android:id="@+id/camera_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/red_and_black_camera_button_icon"
/>
活动:
var changeColor = false
fun cameraButtonTapped() {
changeColor = !changeCameraButtonColor
// I know this doesn't work. This is one of the many ways I tried
binding.camera_button.backgroundTintList = if(changeColor) ColorStateList.valueOf(ContextCompat.getColor(this, R.color.gray)) else null
}
我认为图像最初应该是白色的,
<ImageButton
android:id="@+id/camera_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/red_and_black_camera_button_icon"
app:tint="@color/red"/>
然后你像这样设置所需的颜色
binding.camera_button.setColorFilter(Color.GRAY)