Android的selector 背景选择器
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?> <!-- 默认时的背景图片 --> <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文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg"
或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
ListView.setSelector(drawable);同样的效果。
但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。
其次再来看看Button的一些背景效果:
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"?> <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的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。
转自:http://blog.csdn.net/jrcisme/article/details/6444653
Android的selector 背景选择器的更多相关文章
- Android中selector背景选择器
http://blog.csdn.net/forsta/article/details/26148403 http://blog.csdn.net/wswqiang/article/details/6 ...
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现 首先看到selector的属性: android:state_focus ...
- android中的selector背景选择器的用法
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...
- Android——selector背景选择器的使用详解(二)
在开发应用中,很多情况下要设计listview或button控件的背景,下面总结一下android的selector的用法:1.在drawable中配置Android的selector.将如下的XML ...
- Android RadioButton selector背景
RadioButton selector 背景 <?xml version="1.0" encoding="utf-8"?> <selecto ...
- [JS] selector 背景选择器
用于listview和button改变android原来控件的背景 android的selector是在drawable/xxx.xml中配置的 1.定义xml 把下面的XML文件保存成你自己命名的. ...
- Android:res之selector背景选择器
selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中android:state_focused--------获得焦点a ...
- Android selector背景选择器
selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中 android:state_focused--------获得焦点 ...
- Android:关于背景选择器Selector的item顺序
在使用背景选择器的时候,如果item的顺序不对,会导致不起作用. 1.首先背景选择器的normal选项一定要放在最后. 2.pressed的选择器应该在seclet的前面.我在使用的时候找了半天问题, ...
随机推荐
- SQL Server一些重要视图 1
第一个: sys.indexs 每个堆与索引在它上有一行. 第二个: sys.partitions每个堆与索引的每一个分区返回一行.每一张表最多可以有1000个区. 第三个: sys. allocat ...
- WPF 三态按钮(PNG贴图)
原文 http://blog.csdn.net/power_YQ/article/details/7177183 <Window.Resources> Style x:Key=" ...
- 在Centos 5.x或6.x上安装RHEL EPEL Repo
本文介绍了如何在CentOS 5.x或者CentOS 6.x的系统上使用Fedora Epel repos一个第三方repo:remi资源库.这些资源包并不是天然地支持CentOS,但是提供了很多流行 ...
- Redis用户添加、分页、登录、注册、加关注案例
连接redis代码redis.php <?php //实例化 $redis = new Redis(); //连接服务器 $redis->connect("localhost&q ...
- poj 2540 Hotter Colder 切割多边形
/* poj 2540 Hotter Colder 切割多边形 用两点的中垂线切割多边形,根据冷热来判断要哪一半 然后输出面积 */ #include <stdio.h> #include ...
- DLL导出函数和类 之 __declspec(dllexport)
可利用__declspec(dllexport)导出函数或类. 若要指定C类型约定导出,则需在前面加extern “C”. 若要导出函数,__declspec(dllexport) 关键字必须出现在调 ...
- C#实现给手机发送短信
public static void SendSMS(string FaSongUser, string ToUserList, string ContentStr) { //针对 ...
- IIS7.0/7.5 MVC3 实现伪静态
routes.MapRoute( "Default", "{controller}/{action}.html/{id}&qu ...
- .Net 数组去除重复项
string str = "1/1/12/13/15/16/15//"; ] { '/' }, StringSplitOptions.RemoveEmptyEntries); Li ...
- IO库 8.4
题目:编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中. #include <iostream> #includ ...