使用selector实现按压控件改变背景颜色

可大致分为2步

1.在drawable文件夹下创建以selector为根标签的文件

 点击drawable,右键new --> Drawable resource file,Root element 一栏 填写selector,创建文件。

 指定按下时的颜色或图片  和 非按压时的颜色和图片,如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/color_4AD098" android:state_pressed="true"></item>
    <item android:drawable="@android:color/transparent" android:state_pressed="false"></item>

</selector>

2.在xml中作为控件的background使用

    提醒一下:如果控件是TextView等默认不可点击的控件,不要忘了加上android:clickable="true",如下:

  <TextView
    android:background="@drawable/gray_pressed_bg"
    android:clickable="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
欢迎大家批评指正,谢谢大家。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值