我有一个带有背景图片的ImageButton,它具有一些透明度。默认情况下,由于Holo.Light主题的原因,该按钮会在透明度像素处显示灰色背景。我尝试通过setBackgroundColor(Color.TRANSPARENT)
方法将按钮的背景颜色设置为透明。这工作得很好,做我需要的东西,除了现在我的按钮在聚焦/按下时不再具有浅蓝色,并且看起来相当平坦(没有边框,所以它看起来像一个图像)。Android ImageButton背景色
我GOOGLE了一下,看到你可以按照解释here来指定选择器,但这意味着我必须为每个按钮状态指定一个图像,我不想这样做。我想从主题继承焦点/按下的颜色,但覆盖正常的按钮背景(未按下/聚焦时)为透明而不是灰色。我怎么能做到这一点?请提供一个工作示例,因为我尝试了许多不同的组合,但都没有成功。
编辑 谢谢大家的帮助。我想出了如何使这项工作无需重新创建每个按钮的焦点和按下状态的相同图像!
这里是我的解决方案:
我的按钮被定义为:
<ImageButton
android:id="@+id/ImageButton05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button" />
而且我的背景XML文件(名为button.xml)的定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btn_default_pressed_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btn_default_focused_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item android:state_hovered="true">
<layer-list>
<item android:drawable="@drawable/btn_default_focused_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="@drawable/btn_default_normal_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
</selector>
图像不包含任何灰色..他们是透明的,因为我解释 – Ayyoudy 2012-07-24 02:13:52
的重点是,按下有一个暗蓝色框,而不是灰色。看我的编辑。您必须在上面的图像中编辑出内部的“框”才能留下发光效果。你可以编辑你的问题,并张贴你的imagebutton XML声明之一。也许我误解了你正在做的事情,这会让我更加清楚。 – FoamyGuy 2012-07-24 03:29:11