我的应用中有一个ImageButton
,当按钮按下/聚焦
时,我需要更改图像的色调。我将 ImageButton
设置为从 XML 文件中获取其 src
,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item
android:state_pressed="true"
android:tint="@color/black"
android:drawable="@drawable/search"
/>
<!-- focused -->
<item
android:state_focused="true"
android:tint="@color/black"
android:drawable="@drawable/search"
/>
<!-- default -->
<item
android:tint="@null"
android:drawable="@drawable/search"
/>
</selector>
但是,当 ImageButton
被按下或聚焦时,不会应用色调 - 图像只是正常显示。黑色一如既往地定义为 #000000
。有什么想法吗?
最佳答案
您可以通过以下方式在代码中轻松更改色调:
ImageButton button = (ImageButton) this.findViewById(R.id.button_i_want_to_modify);
button.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
希望对你有帮助。
JS
关于android - 如何在焦点/按下时更改 ImageButton 的色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3024983/