0

A noob quesiton here:

I have a button with a partially-transparent PNG background. I am trying to maintain the transparency of the button across different press states. The problem is: when I apply a gradient to the background image (to indicate the button is pressed), the gradient darkens the whole 'block of space' occupied by the image, not just the image itself.

How do I keep the transparent parts of the image transparent? Thanks in advance...

selector_button_singleplayer.xml

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

gradient_button_singleplayer.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <bitmap android:src="@drawable/button_singleplayer"/>
</item>
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
    </shape>
</item>
</layer-list>

my button:

<Button android:id="@+id/main_menu_choose_single_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dip"
android:background="@drawable/selector_button_singleplayer"
/>
4

1 回答 1

0

去图像按钮

将 android:src 设置为 button_singleplayer

你的 gradient_button_singleplayer.xml 将是

<shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
    </shape>

你的 selector_button_singleplayer.xml 将是

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

你的 imageButton 将是

<ImageButton android:id="@+id/main_menu_choose_single_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dip"
android:background="@drawable/selector_button_singleplayer"
android:src="@drawable/button_singleplayer"
/>
于 2012-06-12T14:29:20.017 回答