android背景选择器selector用法汇总

一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片


http://schemas.android.com/apk/res/android">



android:drawable="@drawable/pic1" />





二.使用xml文件:

1.方法一:在listview中配置android:listSelector="@drawable/xxx

或者在listview的item中添加属性android:background="@drawable/xxx"

2.方法二:Drawable drawable = getResources().getDrawable(R.drawable.xxx);

ListView.setSelector(drawable);但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。

相关属性:

android:state_selected是选中

android:state_focused是获得焦点

android:state_pressed是点击

android:state_enabled是设置是否响应事件,指所有事件

根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。

以下是配置button中的文字效果:

drawable/button_font.xml


http://schemas.android.com/apk/res/android">

Button还可以实现更复杂的效果,例如渐变

drawable/button_color.xml


http://schemas.android.com/apk/res/android">/

//定义当button 处于pressed 状态时的形态。

android:bottom="10dp" android:right="10dp"/>

//定义当button获得 focus时的形态

android:bottom="10dp" android:right="10dp"/>

最后,需要在包含 button的xml文件里添加两项。例如main.xml 文件,需要在里加两项android:focusable="true"android:background="@drawable/button_color"

注:一般触摸屏里面设置android:focusableInTouchMode="true",才能看到这种变化。一般触摸屏里面设置android:focusableInTouchMode="true",才能看到这种变化。

摘自:http://blog.163.com/yeguozhong@yeah/blog/static/1692927492011626103835288/

http://www.devdiv.com/thread-53304-1-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值