用于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. GO1.6语言学习笔记1-基础篇

    一.GO语言优势   可直接编译成机器码,Go编译生成的是一个静态可执行文件,除了glibc外没有其他外部依赖 静态类型语言,但是有动态语言的感觉 语言层面支持并发.Goroutine和channel ...

  2. CheckedComboBoxEditExtension

    public static class CheckedComboBoxEditExtension { public static void BindData(this CheckedComboBoxE ...

  3. Es6 函数式编程 MayBe函子的简单示例

    初级函子的作用非常简单,使用场景主要体现在:深入访问object的属性的时候,不会担心由于属性不存在.undefined.null等问题出现异常. MayBe.js var MayBe = funct ...

  4. macbook基本配置

    1.安装iterm2, 2.安装搜狗输入法, 3.安装迅雷, 4.安装homebrew 5.安装新版的gcc,bash等等,及升级配置文件.

  5. Probability Concepts

    Probability Concepts Unconditional probability and Conditional Probability Unconditional Probability ...

  6. [Linux] 一次SSH认证失败引发的关于通过日志查错误的思考

    一.缘由: 早上在用SSH公钥认证打通所有的机器,有一台机器在完成一些列操作后密钥登陆失败,其他机器一切正常. 错误如下:Public-key authentication with the serv ...

  7. feginclient和ribbon的重试策略

    //自定义重试次数// @Bean// public Retryer feignRetryer(){// Retryer retryer = new Retryer.Default(100, 1000 ...

  8. 使用B或BL跳转时,下一条指令的地址的计算

    .text .global _start 3_start: b step1 step1: ldr pc, =step2 step2: b step2 反汇编代码: : eaffffff b 0x4 : ...

  9. css3实现立方体效果

    <!DOCTYPE html><html><head><meta charset="utf-8" /><title>&l ...

  10. 【查询优化】怎样用SQL语句查看查询的性能指标

    一.SET STATISTICS IO  (有关TSQL语句查询所产生的磁盘活动量) --显示有关由Transact-SQL 语句生成的磁盘活动量的信息 SET STATISTICS IO ON -- ...