android 中实现 点击button ,变换背景

时间:2026-02-13 19:53:57

1、在mian.xml中 定义一个button按钮

其中 background: @drawable/ beforestyle

android 中实现 点击button ,变换背景

2、编写beforestyle.xml,在res 目录下 --找到drawable文件夹 然后 新建beforestyle.xml

文件

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

   

 <!-- 获得焦点但未按下时的背景图片 -->

    <item android:drawable="@drawable/button_use_pre" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>

    <!-- 按下时的背景图片 -->  

    <item android:drawable="@drawable/button_use_pre_highlight" android:state_enabled="true" android:state_pressed="true"/>

    <item android:drawable="@drawable/button_use_pre_highlight" android:state_enabled="true" android:state_focused="true"/>

    <item android:drawable="@drawable/button_use_pre"/>

</selector>

3、要求 提前准备好两张图片,button_use_pre 是没有按下去的图片

button_use_pre_highlight是按下去的图片背景

4、当然,这只是一种实现方法,还可以使用java代码 实现哦(自己可以动手试一试)

© 2026 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com