用于listview和button改变android原来控件的背景

android的selector是在drawable/xxx.xml中配置的

1、定义xml

把下面的XML文件保存成你自己命名的.xml文件,在使用时根据ListView中的列表项的状态(按住/松开/触摸等)来使用相应的背景图片。

drawable/list_item_bg.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>

2、使用xml

(1)listView

① 在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" 使其透明。

(2)Button

再来看看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"?>

<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的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊

[JS] selector 背景选择器的更多相关文章

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

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

  2. Android中selector背景选择器

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

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

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

  4. Android的selector 背景选择器

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

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

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

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

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

  7. Android selector背景选择器

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

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

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

  9. jquery简单原则器(匹配除了指定选择器之外的元素 selector 表示选择器)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. List 集合remove问题

    java的list集合中.使用remove删除元素: 方法一: static List<Integer> list3 = new ArrayList<Integer>(); s ...

  2. Python rfind()方法

    描述 Python rfind() 返回子字符串最后一次出现在字符串中的索引位置,该方法与rindex() 方法一样,只不过如果子字符串不在字符串中不会报异常,而是返回-1. 语法 rfind() 方 ...

  3. jenkins 批量修改配置文件

    jenkins 批量修改配置文件   jenkin job 修改配置 修改前配置 <runPostStepsIfResult> <name>FAILURE</name&g ...

  4. linux权限详解

    一个用户.一个组 我们来看一看 Linux 权限和所有权模型.我们已经看到每个文件属于一个用户和一个组.这正是 Linux 中权限模型的核心.您可以在 ls -l 清单中查看用户和组: $ ls -l ...

  5. hadoop(2.x)以hadoop2.2为例完全分布式最新高可靠安装文档

    问题导读:1.如何配置各个节点之间无密码互通?2.启动hadoop,看不到进程的原因是什么?3.配置hadoop的步骤是什么? 4.有哪些配置文件需要修改?5.如果没有配置文件,该如何找到该配置文件? ...

  6. 《Effective Java》读书笔记四(泛型)

    Java1.5发行版本中增加了泛型(Generic).在没有泛型之前,从集合中读取到的每一个对象都必须进行转换.如果有人不小心插入了错误的类型对象,在运行时的转换处理就会出错.有了泛型之后,可以告诉编 ...

  7. 【Android】16.3 带Intent过滤器的Services

    分类:C#.Android.VS2015: 创建日期:2016-03-01 一.简介 这一节演示带Intent过滤器的Services的基本用法. 1.配置Intent Filter 不论是本地解决方 ...

  8. linux系统信息查询及相关概念

    1.查看机器可以插几根内存条: /sbin/lspci  -v 或dmesg 或  dmidecode(需要root) 2.查看机器有几块盘,是否做raid: blkid 或 fdisk -l 或 s ...

  9. How do I add elements to a Scala List?

    Scala List FAQ: How do I add elements to a Scala List? This is actually a trick question, because yo ...

  10. VC++ 进度条的使用

    进度条控件封装在CProcessCtrl类中 1.SetRange和SetRange32方法来设置进度条显示范围. 语法格式: void SetRange(short nLower, short nU ...