简体   繁体   English

禁用状态时更改ImageButton Drawable Android

[英]Change ImageButton Drawable when state is disabled Android

I want to create a drawable resource to change the image used in an image button when it is disabled. 我想创建一个可绘制资源,以在禁用它时更改图像按钮中使用的图像。

I have created a drawable resource 我创建了一个可绘制的资源

<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>

And then I am referencing it in the android:src="@drawable/resource" . 然后我在android:src="@drawable/resource"引用它。 When I call ImageButton.setEnabled(false) the button is not clickable but the style doesn't change. 当我调用ImageButton.setEnabled(false)该按钮不可单击,但样式不会更改。

android:background is used for other stuff (radius colours etc) android:background用于其他内容(半径颜色等)

Try this 尝试这个

<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>

EDIT : State List 编辑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>

EDIT : 编辑

The order of the items states in the drawable makes a difference. 可绘制对象中项目状态的顺序有所不同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM