原文地址

http://android.blog.51cto.com/268543/564581

首先android的selector是在drawable/xxx.xml中配置的,相关图片放在同目录下。
先看一下listview中的状态
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
<?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:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。
 
=========

Android的selector,背景选择器的更多相关文章

  1. Android中selector背景选择器

    http://blog.csdn.net/forsta/article/details/26148403 http://blog.csdn.net/wswqiang/article/details/6 ...

  2. Android的selector 背景选择器

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

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

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

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

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

  5. Android——selector背景选择器的使用详解(二)

    在开发应用中,很多情况下要设计listview或button控件的背景,下面总结一下android的selector的用法:1.在drawable中配置Android的selector.将如下的XML ...

  6. Android RadioButton selector背景

    RadioButton selector 背景 <?xml version="1.0" encoding="utf-8"?> <selecto ...

  7. [JS] selector 背景选择器

    用于listview和button改变android原来控件的背景 android的selector是在drawable/xxx.xml中配置的 1.定义xml 把下面的XML文件保存成你自己命名的. ...

  8. Android:res之selector背景选择器

    selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中android:state_focused--------获得焦点a ...

  9. Android selector背景选择器

    selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中 android:state_focused--------获得焦点 ...

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

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

随机推荐

  1. main方法执行之前,做什么事

    1.我们知道程序的入口是main方法,那么在执行main方法之前,需要做些什么准备工作呢? 2.main方法执行之前,必须要把non-local static对象构造完成.static对象有:全局对象 ...

  2. Codeforces Round #Pi (Div. 2) A. Lineland Mail 水题

    A. Lineland MailTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/probl ...

  3. Qt一个project调用还有一个project的类成员变量

    一句两句话已经不能表达如今的激动情绪了.唯有感叹知识的博大精深,并把感叹转变为文字. 同一个project调用其它类成员变量很easy. 如: 定义 Test1.h中申明成员变量 class A { ...

  4. ShareSDK for iOS 2.9.0已经公布

    ShareSDK for iOS v2.9.0已经公布,本次更新内容包含: 1.修复Facebook获取用户信息报错问题 2.修复Instagram在iPad上显示分享菜单错误问题,须要指定菜单容器. ...

  5. iOS开发——混编Swift篇&OC移植为swift

    将Ojective-C代码移植转换为Swift代码 2015-03-09 15:07发布:yuhang浏览:201   相比于Objective-C,Swift语言更加简练.有时我们需要把原来写的一些 ...

  6. javascript运行机制之执行顺序详解(转)

    转自http://www.admin10000.com/document/3385.html JavaScript是一种描述型脚本语言,它不同于java或C#等编译性语言,它不需要进行编译成中间语言, ...

  7. LINUX 内核2

    http://blog.csdn.net/acs713/article/category/1363650

  8. discuz常用变量

    帖子URL: {url}帖子标题: {title}附件图片: {pic}帖子内容: {summary}楼主: {author}楼主UID: {authorid}楼主头像: {avatar}楼主头像(中 ...

  9. 如何制作按钮hover状态

    1.选中文字加背景图层 2.Ctrl+J复制图层,向下轻移. 3.点击所复制图层的背景图层,右键选中——混合选项 弹出图层样式框: 4.选择渐变叠加,将渐变——反向勾选上,确定: 5.完成,效果如图.

  10. 用jQuery+easyUI遇到的几个插件与文件详解

    很早就开始跟着老师学习jQuery课程,那时候是要求熟练使用jQuery中的easyUI插件中的控件,包括textbox.combobox.panel.checkbox.tree.datagrid等等 ...