I'd like to change the background for an image button. Basically the image I have looks great on a transparent background and a bit lousy when there are a bunch of them all with a non-transparent background.

This should be easy - just change the android:background on the button to a transparent color (via a drawable): click_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true"><color android:color="#FF008080" />
    </item>
    <item android:state_pressed="true"><color android:color="#FF008080" />
    </item>
    <item><color android:color="#00000000" />
    </item>

</selector>

Then the button

<ImageButton
                android:id="@+id/players"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:background="@drawable/click_background"
                android:contentDescription="@string/players"
                android:src="@drawable/speaker_tile" />

The problem is that I actually want to retain the state_focussed and state_pressed versions of the background (which I presume is derived from the theme). I would like to have the background appear when the button is pressed or selected (and appear with the exact same colour/drawable) that would be used by the theme normally when a button is pressed would be helpful.

I was wondering if there is a way to do one of the following, but have so far been unable to find anything on either:

  1. Define a selector that in some what inherits from another (similar to the way a theme can inherit from another).

  2. Create an entirely new selector but have it's XML reference the color/drawable of the theme's state_focussed and state_pressed used for an image button. Edit: It looks like this option is out. I would have needed attributes, but you can't reference them from a drawable. See here

I'd like to do this in Declarative XML rather than Programmatic Java if possible.

有帮助吗?

解决方案

You can copy the drawables used by the Android OS into your project and use them in your state-list drawable. You can find the images in {android-sdk-directory}/platforms/android-##/data/res/drawable[-*dpi]

EDIT: If you go to {android-sdk-directory}/platforms/android-##/data/res/values, you'll find the themes.xml and styles.xml files used by Android. Using them you can figure out which drawables you'll be looking for.

For example, the default theme on newer versions of Android is Theme.Holo. This theme has a default style for ImageButtons declared like so:

<item name="imageButtonStyle">@android:style/Widget.Holo.ImageButton</item>

In styles.xml, this style is defined as follows:

<style name="Widget.Holo.ImageButton" parent="Widget.ImageButton">
    <item name="android:background">@android:drawable/btn_default_holo_dark</item>
</style>

Thankfully the background attribute is right there defined in plain sight. Sometimes it's inherited from a parent style and you have to find that instead. In any case, here's the drawable (found in the /drawable directory since it's xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
    android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
    android:drawable="@drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true" 
    android:drawable="@drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
    android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
    android:drawable="@drawable/btn_default_disabled_focused_holo_dark" />
<item
     android:drawable="@drawable/btn_default_disabled_holo_dark" />
</selector>

So those are the drawables Android uses for the background of a standard ImageButton in the default (holo dark) theme.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top