我想要一个具有背景图像的按钮,当我单击该按钮时,该按钮的背景或颜色会发生变化(然后我们转到另一项活动。)告诉我如何做到这一点。
创建一个xml
文件并将其保存在drawable
项目中的一个文件夹中,让我们bg.xml
将其设置为您的背景Button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/button_notpressed" />
</selector>
其中,button_pressed
和button_notpressed
是要显示的点击和普通视图的转换图像button
在您的活动xml
文件中,将button
as的背景设置为bg.xml
<Button
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="60dp"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:background="@drawable/bg" <--! set it like this-->
android:text="Submit"
android:textColor="#FFFFFF" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句