在我当前的项目中,我正在使用API 10构建应用程序,该应用程序看起来应该像ICS App。
由于我的GUI不太复杂,因此可以手动构建所有这些,但是按钮状态有些问题。
我在Google上搜索了很多东西,却没有找到对我有用的东西,因此,我将不胜感激;)
我设计了带有Google ICS股票图标的操作栏。例如,当有人触摸搜索放大镜图标时,该图标周围有8dp填充,其背景颜色应更改为定义的突出显示颜色值。
因此,现在您建议使用一个可绘制的选择器。但是我的情况是:我有一个要更改的背景颜色可绘制的图标。我不想更改图标,因此由于无法更改选择器XML的背景色,因此无法正常工作。
(我以为我可以用图标静态bg颜色设置多个drawable-xml,以便我可以用一个选择器引用这些不同的彩色图标,但是由于shape-drawable-xml不能有源,而bitmap-drawable-xml不能有源具有背景色,这不是解决方案。)
如果我尝试使用以下方法在我的onclick-listener(也尝试过onTouch)中更改背景颜色:
ImageButton searchIcon = (ImageButton) findViewById(R.id.upper_actionbar_icon_search);
searchIcon.setBackgroundColor(R.color.android_ics_blue_light);
图标的颜色为深灰色。对于其他任何颜色也是如此,尽管ImageButton上面没有东西。我也尝试了ImageView。
因此,有人可以向我解释一下,我怎么能定义一个突出显示的bg彩色图像而没有任何hacks(位于图标上方并在onclick上可见的默认消失图像)。
非常感谢!!
最佳答案
好的,解决方案很简单。
给自己写一个如下所示的可绘制选择器:
<?xml version="1.0" encoding="utf-8"?>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="@color/android_ics_blue_light"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/android_ics_blue_light"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/light_grey"/>
</shape>
</item>
在ImageButton代码中,将图标定义为src,将新的drawable定义为背景,如下所示:
<ImageButton
<!-- ... -->
android:src="@drawable/icon_search"
android:background="@drawable/actionbar_ontouch"
/>
奇迹般有效!