Kotlin -如何将按钮内图像的色调更改为另一种色调并返回

问题描述 投票:0回答:1

我有一个按钮,里面有一个图像。当按下按钮时,我想将图像色调的颜色更改为灰色,当再次按下按钮时,我想删除灰色。

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
}
android image kotlin button colors
1个回答
0
投票

我认为图像最初应该是白色的,

<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)
© www.soinside.com 2019 - 2024. All rights reserved.