在drawable目录下新建 button_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/clr_normal" android:state_pressed="false" /> <item android:drawable="@drawable/clr_pressed" android:state_pressed="true" /> </selector>
item中的drawable在 value目录下 找到string.xml文件,定义俩个颜色属性
<drawable name="clr_normal">#ff0000</drawable> <drawable name="clr_pressed">#ffffff</drawable>
最后在布局文件中设置backgroud
<Button android:background="@drawable/button_bg"
本文介绍了如何在Android中实现Button点击时改变背景颜色的方法,包括在drawable目录创建button_bg.xml资源,value目录下的string.xml文件定义颜色属性,以及在布局文件中设置背景。
1260

被折叠的 条评论
为什么被折叠?



