Android中的Selector的用法

http://blog.csdn.net/shakespeare001/article/details/7788400#comments

Android中的Selector主要是用来改变ListView和Button控件的默认背景

 one template:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/ic_mymenu_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/ic_mymenu_pressed" /> <!-- focused -->
<item android:drawable="@drawable/ic_mymenu" /> <!-- default -->
</selector>
<?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>

ListView设置点击背景效果,有三种方法可以来引用刚才创建的selector文件:

(1)在ListView中添加如下属性代码
android:listSelector="@drawable/mylist_view"

(2)在ListView的item界面中添加如下属性代码
android:background="@drawable/mylist_view"

(3)利用JAVA代码直接编写
Drawable drawable = getResources().getDrawable(R.drawable.mylist_view);
listView.setSelector(drawable);

为了防止列表拉黑的情况发生,需要在ListView中添加以下的属性代码
android:cacheColorHint="@android:color/transparent"
or
android:cacheColorHint="#00000000"

or

listView.setCacheColorHint(0);

属性介绍:

android:state_selected 选中

android:state_focused 获得焦点

android:state_pressed 点击

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


自定义Button Style

每个button创建的时候都是用的系统默认的风格,如果不满意系统提供的风格的话,你可以用一个 state list drawable 来替代以前的风格.每个state list drawable 是一个drawable resource. 它定义在xml文件中,并且能改变当前的每个状态的背景图.一旦定义了一个自己设计的xml文件,可以在android:background中使用它.

此xml文件中的格式如下,并且很明显发现,一个xml由一个selector元素,包含N个item元素组

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_hovered=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"] />
//例子:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/button_focused" /> <!-- hovered -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

Android中Selector的用法(改变ListView和Button的默认背景)的更多相关文章

  1. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  2. Android中selector的使用

    第一种方法(强烈推荐) 方法:selector做遮罩,原图做background. 我们做按钮的时候经常需要用两个图片来实现按钮点击和普通状态的样式,这就需要提供两种图片,而且每个分辨率下还有多套图片 ...

  3. 【转】 Android中selector的使用

    引言 selector中文的意思选择器,在Android中常常用来作组件的背景,这样做的好处是省去了用代码控制实现组件在不同状态下不同的背景颜色或图片的变换.使用十分方便. selector的定义 s ...

  4. android中sharedPreferences的用法

    SharedPreferences介绍:   做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...

  5. 【转】Android中Application类用法

    转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...

  6. Android中Application类用法

    Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...

  7. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  8. android中sharedPreferences的用法(转)

    SharedPreferences介绍:   做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...

  9. Android中Intent的用法总结

    Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...

随机推荐

  1. openstack系列文章(二)

    学习openstack的系列文章-keystone openstack 架构 Keystone 基本概念 Keystone 工作流程 Keystone Troubleshooting 1.  open ...

  2. PHP.ini 能不能加载子配置文件 ?

    答案是不能,php这个地方用的是另一个方案解决的 编译的时候 用这个参数 --with-config-file-scan-dir指定一个目录 然后在这个目录里面加载ini   https://www. ...

  3. virtualbox命令行启动虚拟机和关闭虚拟机

    C:\Program Files\Oracle\VirtualBox\VBoxManage.exe startvm 虚拟机名字 --type headlessC:\Program Files\Orac ...

  4. xocde missing file 解决方法

    创建一个警告里丢失的文件 托到警报的目录下 重启xcode 警告消失 在xcode中选中这个文件右键delete再次删掉文件,就OK了.

  5. jdk8 Optional使用详解

    思考: 调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法. 原来解决方案: 我们首先要判断这个返回值是否为null,只有在非空的前提下才能将其作为其他方法的参数.这正是一些类似Guav ...

  6. Scrum立会报告+燃尽图(Final阶段第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...

  7. Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValuesByConditionsNoPage

    报Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValu ...

  8. Javascript toString()、toLocaleString()、valueOf()三个方法的区别

    Array.Boolean.Date.Number等对象都具有toString().toLocaleString().valueOf()三个方法,那这三个方法有什么区别??? 一.JS Array 例 ...

  9. 第二阶段Sprint冲刺会议6

    进展:将“录制”及“保存”整合到一起,修复出现的Bug,使之能够正常运行.

  10. Backlog和冲刺结果以及产品Demo市场调研

    Backlog和第一阶段冲刺结果以及产品Demo 博客停更了一段时间,但是我们团队没有闲着,现在一次性汇报团队工作进度,Backlog和第一阶段冲刺结果以及产品Demo. 在一段时间的分工合作以及调整 ...