Android给控件添加默认点击效果


Android控件点击效果

Android中Button控件是有点击效果的,但是像TextView、ImageView、各种Layout是没有点击效果的,给TextView设置点击事件后,加个点击效果用户触控反馈会好一点;

最简单的默认点击效果:

android:background="?android:attr/selectableItemBackground"

如果该空间已经有背景色了,可以设置前景色

android:foreground="?android:attr/selectableItemBackground"

这样一个Android原生的涟漪效果就出来了;

注意:

?android:attr/selectableItemBackgroundBorderless  <!--无边界涟漪效果-->
?android:attr/selectableItemBackground  <!--有边界涟漪效果-->

前景色:foreground属性只能给控件和帧布局设置,其它的布局设置无效果,具体看Android源码;

也可自定义,按照你的需求来:

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

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

    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abc_list_selector_disabled_holo_light" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/abc_list_selector_disabled_holo_light" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/abc_list_selector_background_transition_holo_light" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/abc_list_focused_holo" />
    <item                                                                                          android:drawable="@android:color/transparent" />
</selector>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM