而不会影响pressed和选定的国家

而不会影响pressed和选定的国家

本文介绍了如何修改Android的默认按钮状态,而不会影响pressed和选定的国家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除的ImageButton的背景只有默认状态。我想pssed和选定的国家的$ P $,使它们看起来是正确的在不同设备上使用不同的颜色pssed和选定的国家的$ P $表现如常。

I am trying to remove an ImageButton's background in only the default state. I'd like the pressed and selected states to behave as usual so that they look correct on different devices, which use different colors for the pressed and selected states.

有什么办法来设置的ImageButton的背景默认状态的绘制,而不会影响pressed和选定的国家?

Is there any way to set an ImageButton's background default state's drawable without affecting the pressed and selected states?

我试图用一个选择这样做,但它似乎并没有让您使用默认可绘制一些国家 - 你必须自己设置所有的状态。由于没有API来获取设备的默认pressed /选择可绘,我不知道该怎么设置pressed /选定的国家。

I've tried to do this with a selector, but it does not appear to allow you to use the default drawables for some states - you have to set all the states yourself. Since there's no API to retrieve the device's default pressed/selected drawables, I don't know what to set the pressed/selected states to.

我也尝试过得到StateListDrawable对象,当你不使用一个选择,然后修改它改变了默认状态,该系统创建按钮。这也不起作用。

I also tried getting the StateListDrawable object for the button that the system creates when you are not using a selector and then modify it change the default state. That didn't work either.

我看来,在Android上,如果你想改变绘制的按钮中的一个状态,则必须将所有的状态,因此不能preserve默认可绘制的其他国家。这是正确的?

I seems that on Android, if you want to change the drawable for one state of a button, then you must set all the states, and thus cannot preserve the default drawables for the other states. Is this correct?

谢谢! - 汤姆湾

推荐答案

汤,

这是真的,如果你覆盖默认状态下,你还必须重写pressed和专注的状态。其原因是,默认的机器人绘制的是一个选择,所以覆盖它与你失去的状态信息pressed和聚焦状态的静态绘制手段,因为你只为它指定的一个绘制。这是超级容易实现自定义选择,虽然。做这样的事情:

It's true that if you override the default state you also have to override the pressed and focused states. The reason is that the default android drawable is a selector, and so overriding it with a static drawable means that you lose the state information for pressed and focused states as you only have one drawable specified for it. It's super easy to implement a custom selector, though. Do something like this:

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

将这个你可绘目录,并加载它像一个正常的绘制的的ImageButton的背景。最难的部分,我是设计的实际图像。

Put this in your drawables directory, and load it like a normal drawable for the background of the ImageButton. The hardest part for me is designing the actual images.

编辑:

只是做了一些深入挖掘的EditText的来源,这是他们如何设置背景绘制:

Just did some digging into the source of EditText, and this is how they set the background 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);
}

我相信你能够适应的想法你的目的。下面是我发现它在页面上:

I'm sure you could adapt the idea to your purposes. Here is the page I found it on:

这篇关于如何修改Android的默认按钮状态,而不会影响pressed和选定的国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:29