在android中动态更改背景或前景按钮颜色
假设我的应用程序中有一个类似以下内容的页脚,它是在XML文件(如footer.XML)中定义的:在android中动态更改背景或前景按钮颜色,android,xml,button,colors,onclick,Android,Xml,Button,Colors,Onclick,假设我的应用程序中有一个类似以下内容的页脚,它是在XML文件(如footer.XML)中定义的: <LinearLayout android:id="@+id/llfooter" android:layout_weight="1" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="0dp" android:vi
<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 drawables(),如下所示
<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个具有更改背景的按钮(因此更多地使用graph editor和.apk,因为更多的光栅图标…)。
有没有办法创建(甚至在java中)浅颜色的“onClick”和普通颜色的“onRelease”,在参考资料中每个特性只有一个图标
有什么建议吗
提前通知Tnx。
Gabo使用ImageButton,并将android:src参数设置为具有透明背景的按钮可绘制,然后将android:background值设置为选择器可绘制,例如,选择时可更改颜色
这样,你就有了一套图标的可绘制功能,一套只可绘制背景,背景会根据按钮的状态而变化,你可以在代码中的onTouch中获得按钮位图并更改颜色,但这不是个好主意。
选择器是最好的解决方案。非常感谢您的正确答案,Jucas。它现在就像我想要的那样工作@加博兰德请选择这个答案作为正确答案,如果这个答案解决了你的问题