具有禁用UI感觉的Android ImageButton

44 android image

我有一个禁用的ImageButton(不可点击或设置为禁用).我想给用户一种UI感觉,即在不使用任何其他图像的情况下禁用它.

有没有办法做到这一点?

Shl*_*blu 59

不同于一般的Button,一个ImageButtonButton停用时具有图像背景不再是灰色.您实际上必须使用另一个图像或以显示为灰色的方式处理它.

如果使用另一张图片就可以了,你可以使用a <selector>(这里与常规相关Button但这个相关的相同):

  • /drawable/my_selector.xml:

    <?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/button_gray" /> ***button_gray is a Drawable image***
        <item android:state_pressed="true"
            android:drawable="@drawable/button_gray" /> 
        <item android:drawable="@drawable/button_red" /> ***button_red is a Drawable image*** 
    </selector>
    
    Run Code Online (Sandbox Code Playgroud)

请注意,在选择器中,逻辑应用顺序方式,每个项目的项目.这里button_red一直使用,但是当按钮被禁用或被按下时.

  • 你的layout.xml:

    <Button android:id="@+id/myButton"
            android:background="@drawable/my_selector" ***this is a reference to the selector above ***
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    />
    
    Run Code Online (Sandbox Code Playgroud)

如果使用其他图像成为问题,其他答案(例如@Tronman或@southerton)会为您提供以编程方式处理图像的方式,使其显示为灰色.


tro*_*man 35

@Oleg Vaskevich在这里给出了一个不同的解决方案:禁用ImageButton

他的解决方案允许您在ImageButton不创建其他图像或使用的情况下灰显<selector>.

/**
 * Sets the image button to the given state and grays-out the icon.
 * 
 * @param ctxt The context
 * @param enabled The state of the button
 * @param item The button item to modify
 * @param iconResId The button's icon ID
 */
public static void setImageButtonEnabled(Context ctxt, boolean enabled, 
        ImageButton item, int iconResId) {

    item.setEnabled(enabled);
    Drawable originalIcon = ctxt.getResources().getDrawable(iconResId);
    Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon);
    item.setImageDrawable(icon);
}

/**
 * Mutates and applies a filter that converts the given drawable to a Gray
 * image. This method may be used to simulate the color of disable icons in
 * Honeycomb's ActionBar.
 * 
 * @return a mutated version of the given drawable with a color filter applied.
 */
public static Drawable convertDrawableToGrayScale(Drawable drawable) {
    if (drawable == null) 
        return null;

    Drawable res = drawable.mutate();
    res.setColorFilter(Color.GRAY, Mode.SRC_IN);
    return res;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案,因为它解决了op不使用其他图像的要求.谢谢你的解决方案. (3认同)

Sno*_*ugg 10

我更喜欢覆盖setEnabled()ImageButton中的方法来相应地更改图像的alpha属性。因此,当按钮被禁用时,图像将部分透明并且看起来更残废。

public class CustomImageButton extends ImageButton {
    //...

    @Override
    public void setEnabled(boolean enabled) {
        if(this.isEnabled() != enabled) {
            this.setImageAlpha(enabled ? 0xFF : 0x3F);
        }
        super.setEnabled(enabled);
    }
}
Run Code Online (Sandbox Code Playgroud)


sou*_*ton 7

详细说明@tronman答案你还可以编写一个函数,它会使动态加载的drawable变灰(即不是来自资源,例如从原始svg文件加载并在运行中转换为BitmapDrawables).

/**
 * Sets the specified image buttonto the given state, while modifying or
 * "graying-out" the icon as well
 *
 * @param enabled The state of the menu item
 * @param item The menu item to modify
 * @param originalIcon The drawable
 */
public static void setImageButtonEnabled(Context ctxt, boolean enabled, ImageButton item, Drawable originalIcon) {
    item.setEnabled(enabled);

    Drawable res = originalIcon.mutate();
    if (enabled)
        res.setColorFilter(null);
    else
        res.setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
}
Run Code Online (Sandbox Code Playgroud)

如果你在背景上也有一个不透明的drawable(用android:background设置),请参阅选择器Android:如何使Drawable Selector也修改背景.


小智 6

在 ImageButton 上使用 setImageAlpha 可以完成此操作

启用时,

 ((ImageButton) findViewById(R.id.btnImageButton1)).setEnabled(true);
 ((ImageButton) findViewById(R.id.btnImageButton1)).setImageAlpha(0xFF);
Run Code Online (Sandbox Code Playgroud)

禁用时,

 ((ImageButton) findViewById(R.id.btnImageButton1)).setEnabled(false);
 ((ImageButton) findViewById(R.id.btnImageButton1)).setImageAlpha(0x3F);
Run Code Online (Sandbox Code Playgroud)

正如 @SnoopDougg 建议的那样,自定义 ImageButton 类可能是扩展 ImageButton 并在其中设置 ImageAlpha 的好主意;还没有尝试过但是。