Android按钮 - 在新闻状态下保持图像透明度

时间:2012-06-12 14:05:29

标签: android android-layout android-button

这里有一个菜鸟问题:

我有一个部分透明的PNG背景按钮。我试图在不同的印刷状态下保持按钮的透明度。问题是:当我对背景图像应用渐变时(表示按下按钮),渐变会使图像占据的整个“空间块”变暗,而不仅仅是图像本身。

如何保持图像的透明部分透明?提前谢谢......

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>

我的按钮:

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

1 个答案:

答案 0 :(得分:0)

转到ImageButton

将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"
/>