以编程方式用作背景时如何更改 ImageButton 可绘制颜色?
How to change ImageButton drawable color when used as background programmatically?
我有以下 ImageButton:
<ImageButton
android:id="@+id/nopeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/nope_button_states" />
这里是nope_button_states
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/nope_button" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/nope_button_selected" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/nope_button_selected" />
<item
android:state_enabled="true"
android:drawable="@drawable/nope_button" />
</selector>
现在我想更改可绘制对象的色调。当我使用时:
android:src="@drawable/nope_button"
然后我可以这样做:nopeButton.setColorFilter()
但是在后台使用时这不再有效。
如何在与背景一起使用时更改 ImageButton 绘图的色调 属性?
尝试使用:
nopeButton.getBackground().setColorFilter();
我有以下 ImageButton:
<ImageButton
android:id="@+id/nopeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/nope_button_states" />
这里是nope_button_states
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/nope_button" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/nope_button_selected" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/nope_button_selected" />
<item
android:state_enabled="true"
android:drawable="@drawable/nope_button" />
</selector>
现在我想更改可绘制对象的色调。当我使用时:
android:src="@drawable/nope_button"
然后我可以这样做:nopeButton.setColorFilter()
但是在后台使用时这不再有效。
如何在与背景一起使用时更改 ImageButton 绘图的色调 属性?
尝试使用:
nopeButton.getBackground().setColorFilter();