【问题标题】:How to modify the default button state in Android without affecting the pressed and selected states?如何在不影响按下和选择状态的情况下修改Android中的默认按钮状态?
【发布时间】:2010-10-04 21:12:06
【问题描述】:

我正在尝试仅在默认状态下删除 ImageButton 的背景。我希望按下和选定状态像往常一样运行,以便它们在不同设备上看起来正确,这些设备对按下和选定状态使用不同的颜色。

有什么方法可以设置 ImageButton 的背景默认状态的可绘制而不影响按下和选择状态?

我尝试使用选择器来执行此操作,但它似乎不允许您对某些状态使用默认可绘制对象 - 您必须自己设置所有状态。由于没有 API 可以检索设备的默认按下/选定的可绘制对象,因此我不知道将按下/选定状态设置为什么。

我还尝试在您不使用选择器时为系统创建的按钮获取 StateListDrawable 对象,然后对其进行修改以更改默认状态。那也没用。

我似乎在Android上,如果您想更改按钮的一种状态的drawable,那么您必须设置所有状态,因此无法保留其他状态的默认drawable。这是正确的吗?

谢谢! -汤姆 B.

【问题讨论】:

    标签: android button default selector


    【解决方案1】:

    汤姆,

    确实,如果您覆盖默认状态,您还必须覆盖按下和聚焦状态。原因是默认的 android 可绘制对象是一个选择器,因此用静态可绘制对象覆盖它意味着您丢失了按下和聚焦状态的状态信息,因为您只为它指定了一个可绘制对象。不过,实现自定义选择器非常容易。做这样的事情:

    <selector
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/custombutton">
    
        <item
            android:state_focused="true"
            android:drawable="@drawable/focused_button" />
        <item
            android:state_pressed="true"
            android:drawable="@drawable/pressed_button" />
        <item
            android:state_pressed="false"
            android:state_focused="false"
            android:drawable="@drawable/normal_button" />
    </selector>
    

    把它放在你的drawables目录中,然后像一个普通的drawable一样加载它作为ImageButton的背景。对我来说最困难的部分是设计实际图像。

    编辑:

    刚刚深入研究了EditText的来源,这就是他们设置背景drawable的方式:

    public EditText(/*Context context, AttributeSet attrs, int defStyle*/) {
        super(/*context, attrs, defStyle*/);
    
                StateListDrawable mStateContainer = new StateListDrawable();
    
                ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
                pressedDrawable.getPaint().setStyle(Paint.FILL);
                pressedDrawable.getPaint().setColor(0xEDEFF1);
    
    
                ShapeDrawable focusedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
                focusedDrawable.getPaint().setStyle(Paint.FILL);
                focusedDrawable.getPaint().setColor(0x5A8AC1);
    
                ShapeDrawable defaultDrawable = new ShapeDrawable(new RoundRectShape(10,10));
                defaultDrawable.getPaint().setStyle(Paint.FILL);
                defaultDrawable.getPaint().setColor(Color.GRAY);
    
    
    
                mStateContainer.addState(View.PRESSED_STATE_SET, pressedDrawable);
                mStateContainer.addState(View.FOCUSED_STATE_SET, focusedDrawable);
                mStateContainer.addState(StateSet.WILD_CARD, defaultDrawable);
    
                this.setBackgroundDrawable(mStateContainer);
    }
    

    我相信你可以根据你的目的调整这个想法。这是我找到它的页面:

    http://www.google.com/codesearch/p?hl=en#ML2Ie1A679g/src/android/widget/EditText.java

    【讨论】:

    • 感谢您的回复丹。这确实回答了我关于是否必须提供选择器的所有状态的具体问题,不幸的是你这样做了。看起来操作系统根据您发布的 EditText 源对按下/选择的颜色进行了硬编码。我将提出一个单独的问题,即应用程序是否可以确定设备上默认按下/选择的颜色是什么。
    【解决方案2】:
    StateListDrawable replace = new StateListDrawable();
    
    Drawable old = getBackground();
    replace.addState(FOCUSED_STATE_SET, old);
    replace.addState(SELECTED_STATE_SET, old);
    replace.addState(PRESSED_STATE_SET, old);
    
    replace.addState(StateSet.WILD_CARD, new ColorDrawable(Color.TRANSPARENT));
    
    if (Build.VERSION.SDK_INT >= 16) {
        setBackground(replace);
    } else {
        setBackgroundDrawable(replace);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 2014-01-14
      • 2018-09-04
      • 1970-01-01
      相关资源
      最近更新 更多