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

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片-->
<item android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片 -->
<item android:state_window_focused="false"
android:drawable="@drawable/pic1" />
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" /> <!-- 触摸模式下单击时的背景图片--> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" /> <!--选中时的图片背景--> <item android:state_selected="true" android:drawable="@drawable/pic4" /> <!--获得焦点时的图片背景--> <item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>

二.使用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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>
Button还可以实现更复杂的效果,例如渐变
drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> /
<item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。
<shape> <gradient android:startColor="#8600ff" /> <stroke android:width="2dp" android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp"/>
</shape> </item>
<item android:state_focused="true">//定义当button获得 focus时的形态
<shape>
<gradient android:startColor="#eac100"/>
<stroke android:width="2dp" android:color="#333333" color="#ffffff"/>
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp"/>
</shape>
</item>
</selector>
最后,需要在包含 button的xml文件里添加两项。例如main.xml 文件,需要在<Button />里加两项android:focusable="true" android:background="@drawable/button_color"

【Android】Android背景选择器selector用法汇总的更多相关文章

  1. android背景选择器selector用法汇总

    一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...

  2. AndroidA——背景选择器selector用法汇总(一)

    一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...

  3. Android:关于背景选择器Selector的item顺序

    在使用背景选择器的时候,如果item的顺序不对,会导致不起作用. 1.首先背景选择器的normal选项一定要放在最后. 2.pressed的选择器应该在seclet的前面.我在使用的时候找了半天问题, ...

  4. [转]永久告别Android的背景选择器Selector!无需切很多图了!

    package com.zoke.custom.autobg; import android.content.Context; import android.content.res.TypedArra ...

  5. android中的selector背景选择器的用法

    关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...

  6. Android View 背景选择器编写技巧

    在项目中选择器的使用是非常多的,以下是本人在项目中的一些常用的背景选择器的写法 带边框下划线背景选择器效果图: 上面布局中放了10个CheckBox,然后设置了CheckBox的背景图片位,背景选择器 ...

  7. Android selecter背景选择器使用

    android:drawable这个属性是必须的,默认时的背景图片. android:state_pressed布尔值.true指当用户点击或者触摸该控件的状态.默认为false android:st ...

  8. Android dp和sp的用法汇总

    1 > dp 是跟像素密度无关的单位,也就是说在相同尺寸.但不同的分辨率的手机上,用dp标识的东西,显示的大小是一样的. sp是用于标识字体的,它不仅跟屏幕尺寸有关,还跟设置的系统字体大小有关. ...

  9. android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现

              android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现 首先看到selector的属性: android:state_focus ...

随机推荐

  1. EasyUI的treegrid组件动态加载数据问题的解决办法

    http://www.jquerycn.cn/a_3455 —————————————————————————————————————————————————————————————————————— ...

  2. 上手并过渡到PHP7(4)——取代fatal error的engine exceptions

    上手并过渡到PHP7 取代fatal error的engine exceptions 泊学原文链接泊学代码秀视频 自从PHP 4以来,PHP的错误处理几乎就是一成不变的.只不过在PHP 5.0里添加了 ...

  3. jQuery 中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

  4. TCP/IP和Socket的关系

    要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...

  5. eclipse的Maven项目pom.xml错误信息提示missingxxxjar解决方案

    今天在学习的时候需要用到maven工程,当时找完所依赖的包的三要素就开始下载了,写完pom.xml需要一段时间下载这些jar包,就躺在一边等了.可能是笔记本有节能功能这个原因导致我醒来时断网发现满屏m ...

  6. pyqt二进制和图片的转换

    参考:http://blog.chinaunix.net/uid-28194872-id-3516936.html MySQL数据库要想插入图片,其字段需要是BLOB类型.BLOB (binary l ...

  7. C++字符串类型和数字之间的转换

    转载:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 字符串---字符数组(1)string ...

  8. MathType公式编辑器快捷键操作

    快捷键操作是最常见的操作方式,MathType软件系统提供大量的快捷键操作供用户使用.使用MathType公式编辑器快捷键操作可节省大量的操作的时间,本教程将详解MathType快捷键操作. 放大或缩 ...

  9. 放在github pages上的静态网站怎么取消绑定自定义域名?

    使用GitHub Pages搭建的静态网站绑定了自定义域名操作,但是想反悔怎么办? 删除CNAME文件,重新删除仓库,新建后均不行~ 解决办法:清除一下浏览器缓存,或者用别的浏览器打开就好了,因为之前 ...

  10. css hack整理 (摘)

    CSS Hack Table     Y 渲染 N 不渲染 H 部分版本或部分属性渲染 B 样式失效   windows Mobile Linux Mac IE Firefox Chrome Safa ...