开发者

Android: How to 'grey out' a radiobutton

开发者 https://www.devze.com 2023-01-13 17:23 出处:网络
Disabling a radiobutton does not \'grey 开发者_如何学JAVAout\' the control in the same way as disabling a checkbox.

Disabling a radiobutton does not 'grey 开发者_如何学JAVAout' the control in the same way as disabling a checkbox. I was thinking, how difficult would it be to place a semi-transparent black bitmap over the whole radiobutton view, thereby making it appear darker?

Not really sure where to start on this one, any ideas?


Adding a veneer on an existing image or drawable is best done with a ColorFilter.

You could create two separate drawables and add them to a StateListDrawable, and give the disabled drawable a grey color filter. That could cost you some memory though, since you're duplicating a drawable, and it sounds like you're just using Android's stock radio button anyway so creating a copy of it might be pain.

A more universal way of doing it is just to apply the color filter whenever the View is disabled. Try subclassing the radio button and overriding drawableStateChanged() like so:

@Override
public void drawableStateChanged() {
    Drawable background = getBackground();
    if(background != null) {
        if(!isEnabled()) {
            background.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        } else {
            background.setColorFilter(null);
        }
    }
}

Edit:
Uh-oh, I see that CompoundButton has a method setButtonDrawable(). So the compound buttons must not use the background as their drawables, like a normal button does. And of course mButtonDrawable is a private variable and there's no equivalent 'get' method -- thanks a lot Google. Short of accessing it via reflection, you may have to create your own drawable resource for this one. :-(

Creating your own button drawable gives you more control over the look of your app, but of course it means that you won't match the theme of whatever device you're running on, plus it adds to the size of your app because you end up adding like 8 png resources to it. You could try overriding onDraw() like you talked about, and putting some kind of semi-transparent ColorDrawable on top of it, but every time I've tried that myself it looked like complete crap.

Before going down either of those roads, try this: Luckily, the radio button drawable is exposed in the public API at android.R.drawable.btn_radio. So, you could inflate this into a StateListDrawable and try adding states to it somehow. One way that might work is this:

    StateListDrawable draw = (StateListDrawable) getResources().getDrawable(android.R.drawable.btn_radio);
    Drawable radioBtnNormal = draw.getCurrent();
    Drawable radioBtnDisabled = new LayerDrawable(new Drawable[]{radioBtnNormal});
    radioBtnDisabled.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
    draw.addState(new int[]{-android.R.attr.state_enabled}, radioBtnDisabled);
    button.setButtonDrawable(draw);

You may have to play around with the state set on the second-to-last line there; I always have trouble getting the states just right so that it does what I want. Also notice the negative sign in front of state_enabled, which indicates the disabled state.

If this doesn't work for you, you could also try wrapping the original drawable in your own subclass of LayerDrawable and overriding the onStateChanged method to add/remove the color filter based on your state.

I have done similar things but not this specific thing, so I don't know if it'll work, but hopefully I've given you enough ideas that you'll find something that works for you!

0

精彩评论

暂无评论...
验证码 换一张
取 消