我已经设置了一个drawable到我的ImageButton的色调,所以当启用或禁用按钮时,图标的颜色会自动更改,有没有办法以编程方式更改色调,所以我保持相同的行为只是使用不同的颜色启用和禁用状态?
my_layout.xml的内容:
<ImageButton
android:id="@+id/button_minus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@color/default_button_background"
android:tint="@drawable/button_tint_color"
app:srcCompat="@drawable/ic_remove_24px" />
button_tint_color.xml的内容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:color="@color/icon_tint_disable_color" />
<item
android:color="@color/icon_tint_enable_color" />
</selector>
然后在我的代码中我可以做buttonMinus.setEnabled(true)
或buttonMinus.setEnabled(false)
并自动更改图标颜色。有没有办法以编程方式为启用的颜色或禁用的颜色中的一个或两个设置不同的颜色?
你可以使用这样的东西我想:
if(buttonMinus.isEnabled()){
//With button enabled
yourIcon.setItemIconTintList(ColorStateList.ValueOf(yourColor));
}else{...}
到目前为止我发现的最好的方法是以编程方式创建一个新的颜色状态列表并将其分配给按钮,yikes,目标是避免以编程方式设置颜色等视觉属性...
ColorStateList buttonStates = new ColorStateList(
new int[][] {
{ -android.R.attr.state_enabled },
{}
},
new int[] {
Color.RED,
Color.BLUE
}
);
buttonMinus.setImageTintList(buttonStates);