在禁用状态Android时更改ImageButton Drawable

时间:2018-03-26 12:56:19

标签: android android-layout android-drawable android-imagebutton

我想创建一个可绘制资源来更改图像按钮禁用时使用的图像。

我创建了一个可绘制资源

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <bitmap android:src="@drawable/enabled_image" />
    </item>
    <item android:state_enabled="false">
        <bitmap android:src="@drawable/disabled_image" />
    </item>
</selector>

然后我在android:src="@drawable/resource"中引用它。当我拨打ImageButton.setEnabled(false)时,该按钮无法点击,但样式不会改变。

android:background用于其他东西(半径颜色等)

2 个答案:

答案 0 :(得分:2)

试试这个

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/disabled_backgroungd"/>
    <item android:state_pressed="false"
        android:drawable="@drawable/normal_backgroungd" />
    <item android:state_pressed="true"
        android:drawable="@drawable/backgroungd_selected" />
    <item android:state_enabled="true" android:drawable="@drawable/normal_backgroungd"/> 
</selector>

编辑State List

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

修改

drawable中项目状态的顺序有所不同。

答案 1 :(得分:0)

if(!ImageButton.isEnabled())
{
     //set the drawable         
}