【问题标题】:ImageButton doesn't highlight on click with Transparent backgroundImageButton 在点击透明背景时不突出显示
【发布时间】:2012-10-01 07:03:54
【问题描述】:

我已将 ImageButton 设置为透明的,因此图标与 Android ActionBar 等背景面板相匹配。这看起来不错,如我所愿。

但是,当背景透明时,不会像在操作栏中按下透明按钮时那样看到蓝色高亮。

我可以有一个透明的 ImageButton 并且在单击时高亮闪烁吗?

<ImageButton
    android:id="@+id/nextItemButton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:src="@drawable/ic_media_ff" />

【问题讨论】:

    标签: android button transparency


    【解决方案1】:

    您需要做的就是设置合适的背景。如果您希望它在正常状态下透明而在按下状态下呈蓝色。

    res/drawable 目录中创建一个像这样的StateListDrawable

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/my_bluish_color" android:state_pressed="true"/>
        <item android:drawable="@android:color/transparent"/>
    </selector>
    

    这样默认背景是透明的。按下时,背景具有您指定的颜色(您可以在此处使用任何可绘制对象来代替颜色)。

    【讨论】:

    • 不幸的是,内置 android 样式中的按钮突出显示不公开。这是我最终使用的自定义颜色。尽可能接近果冻豆的颜色,我认为这与 ICS 中的颜色相同:#057891 使用 StateListDrawable 效果很好。
    • 默认按下的颜色取决于设备及其运行的 Android 版本。 Holo 主题在按下状态下使用半透明蓝色 9-patch 可绘制对象。
    【解决方案2】:

    如果您想以编程方式执行此操作,这是一种解决方案:

    创建自定义ImageButton 类并覆盖drawableStateChange()

    public class CustomImageButton extends ImageButton {
    
        @Override
        protected void drawableStateChanged() {
            Log.d("Button", "isPressed: " + isPressed() );
            if( isPressed() ){
                setBackgroundResource( android.R.color.holo_blue_dark );
            }  else {
                setBackgroundResource( android.R.color.transparent );
            }
            super.drawableStateChanged();
    
        }
    
        public CustomImageButton( Context context ) {
            super( context );
        }
    
        public CustomImageButton( Context context, AttributeSet attrs ) {
            super( context, attrs );
        }
    
        public CustomImageButton( Context context, AttributeSet attrs, int defStyle ) {
            super( context, attrs, defStyle );
            // TODO Auto-generated constructor stub
        }
    
    
    
    }
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。最后,我得到了一个具有该属性的代码示例:

      android:background="?android:selectableItemBackground"
      

      此属性将为任何视图(按钮、ImageButton、TextView...)提供带有可选高亮显示的透明背景无需更多编码!!!

      【讨论】:

      • 如果您使用的是 ActionBarSherlock,请改用 android:background="?selectableItemBackground"。
      • 这可以通过编程方式设置为视图吗?
      • 很好的解决方案,但需要 Api 级别 11 :/
      • 对于那些想知道的人来说,这存在于 attrs... 就我个人而言,我会在完全限定的状态下使用它 "?android:attr/selectableItemBackground" 以保持它的可读性
      • 这应该被选为最佳答案。
      【解决方案4】:

      只是补充费尔南德斯的好答案:

      如果您希望效果是圆形而不是矩形,请使用:

      android:background="?android:selectableItemBackgroundBorderless"    
      

      (*适用于 V21 及更高版本)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 2013-03-30
        • 2017-10-07
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多