Android中Selector的用法(改变ListView和Button的默认背景)
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的默认背景)的更多相关文章
- android 中uri.parse()用法
		
android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...
 - Android中selector的使用
		
第一种方法(强烈推荐) 方法:selector做遮罩,原图做background. 我们做按钮的时候经常需要用两个图片来实现按钮点击和普通状态的样式,这就需要提供两种图片,而且每个分辨率下还有多套图片 ...
 - 【转】 Android中selector的使用
		
引言 selector中文的意思选择器,在Android中常常用来作组件的背景,这样做的好处是省去了用代码控制实现组件在不同状态下不同的背景颜色或图片的变换.使用十分方便. selector的定义 s ...
 - android中sharedPreferences的用法
		
SharedPreferences介绍: 做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...
 - 【转】Android中Application类用法
		
转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...
 - Android中Application类用法
		
Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...
 - (转)Android中Parcelable接口用法
		
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
 - android中sharedPreferences的用法(转)
		
SharedPreferences介绍: 做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...
 - Android中Intent的用法总结
		
Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...
 
随机推荐
- Maven ResourceBundle.getBundle读取Properties异常MissingResourceException: Can't find bundlei解决方法
			
参考:https://blog.csdn.net/thousa_ho/article/details/72817616 问题描述 ResourceBundle读取properties配置文件提示 Mi ...
 - OSSEC 架构
			
OSSEC由很多部分组成,它有一个集中的管理端,用于监控.并接收来自代理.syslog.数据库或无代理设备的日志. 管理端(服务器) 管理端属于OSSEC部署中的中心部分.它存储了文件完整性检测数据库 ...
 - Windows本地上传源码到Gitee远程仓库
			
1.下载Git,并安装. 安装时一路默认即可 https://git-scm.com/downloads 验证Git安装成功否 cmd 下输入,出现版本号即成功 git --version 2.生成s ...
 - 函数式编程与React高阶组件
			
相信不少看过一些框架或者是类库的人都有印象,一个函数叫什么creator或者是什么什么createToFuntion,总是接收一个函数,来返回另一个函数.这是一个高阶函数,它可以接收函数可以当参数,也 ...
 - Scrum Meeting 3  -2014.11.5
			
这几天小伙伴们都在努力,研究出不少改进方案并加以设计和实施了,分词算法的优化进度可观,而其他的任务在改进的过程中产生了些问题,对于之前代码的设计感到疑惑,我们找到了上届的学长们咨询,他们也给出了不少建 ...
 - Daily Scrum 11.19   部分测试报告
			
下面是我们的部分测试报告: 功能测试部分: 1Exception in thread "Thread-11" java.lang.IllegalArgumentException: ...
 - “Gogoing”改进方案
			
通过看见他们对我们团队的意见点评,我们还要有更多改善的地方. 首先,就是界面的优化: 其次,加上自己些特有的功能,吸引更多的用户: 然后,需要整理大量的数据库信息才能完善: 最后,需要有其他软件的集成 ...
 - Chapter 1 概述
			
软件分为通用软件和应用软件,具有复杂.不可见和不断变化的特点:从出现到现在共经历了四个重要的发展阶段,但现在的软件发展依然存在着许多问题. 软件工程是采用工程的概念.原理.技术和方法来开发与维护软件, ...
 - beta发布的评论
			
1. 组名:飞天小女警 项目名:礼物挑选小工具 评价:对于我们学生来说,选礼物小工具相对来说新颖,小组添加了前十名热门礼物的推荐.发布到服务器上了,未来也有很多选择的空间可以做成礼物挑选手机app,也 ...
 - python 菜鸟入门
			
python 菜鸟博客: http://www.cnblogs.com/wupeiqi/articles/5433893.html http://www.cnblogs.com/linhaifeng/ ...