2012-08-06 31 views
0

假设我有一个像我的应用程序下,在一个XML文件中定义的诸如footer.xml页脚动态变化的背景或前景的按钮颜色:现在android系统

<LinearLayout android:id="@+id/llfooter" 
     android:layout_weight="1" android:layout_width="fill_parent" 
     android:orientation="horizontal" android:layout_height="0dp" 
     android:visibility="visible" android:background="@drawable/fbg" 
     android:weightSum="5.0" android:gravity="center" 
     android:layout_margin="0dp"> 

     <Button android:id="@+id/home" android:layout_height="wrap_content" 
      android:layout_width="wrap_content" android:layout_weight="1" 
      android:background="@drawable/home" android:textColor="#FFFFFF" 
      android:padding="10px"></Button> 

     <Button android:id="@+id/issue" android:layout_height="wrap_content" 
      android:layout_width="wrap_content" android:layout_weight="1" 
      android:background="@android:drawable/ic_menu_send" android:textColor="#FFFFFF" 
      android:padding="10px"></Button> 

     <Button android:id="@+id/browse" android:layout_height="wrap_content" 
      android:layout_width="wrap_content" android:layout_weight="1" 
      android:background="@android:drawable/ic_menu_slideshow" android:textColor="#FFFFFF" 
      android:padding="10px"></Button> 

     <Button android:id="@+id/search" android:layout_height="wrap_content" 
      android:layout_width="wrap_content" android:layout_weight="1" 
      android:background="@drawable/search" android:textColor="#FFFFFF" 
      android:padding="10px"></Button> 

     <Button android:layout_height="wrap_content" android:id="@+id/favorite" 
      android:background="@drawable/favorite" android:layout_width="wrap_content" 
      android:focusable="true" 
      android:clickable="true" 
      android:layout_weight="1" 
      android:textColor="#FFFFFF" android:padding="10px"></Button> 
    </LinearLayout> 

,问题是,问题,浏览等是PNG图标,当我点击它们时,用户不能有反馈,因为它们保持不变。

我想在按下它们时改变背景颜色(例如稍微淡一点)。我知道我可以写下来XML可绘制(),每个按钮一个,如下面的

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_focused="true" android:state_pressed="true" 
      android:drawable="@drawable/bgalt" /> 
     <item android:state_focused="false" android:state_pressed="true" 
      android:drawable="@drawable/bgalt" /> 
     <item android:drawable="@drawable/bgnorm" /> 
    </selector> 

..但如果我有10个按钮(比如,5尾,5头)我应该创建等10个按钮改变了背景(所以更多的图形编辑工作和.apk因为更多的栅格图标而变得更重。)。 有没有一种方法可以创建(即使在java中)一个更简单的颜色“onClick”和普通颜色“onRelease”,而每个功能的资源只有一个图标?

有什么建议吗?

Tnx提前。 加博

回答

0

使用的ImageButton,并设置机器人:src参数到按钮可绘制具有透明背景,然后设置机器人:背景值到选择器抽拉选自例如当改变颜色。

您有一组可绘制你的图标和仅适用于更改背景一个绘制的这样,根据您的按钮的状态

+0

非常感谢您的正确答案,Jucas。它就像我现在想要的那样工作! – gabolander 2012-08-07 08:41:57

+0

@gabolander请选择这个答案作为正确的答案,如果这个答案解决了你的问题 – Bhargav 2015-10-23 09:40:11

0

你可以得到按钮位图onTouch代码和变色,但这是个坏主意。 选择器是最佳解决方案。