Selector-背景选择器

1.主要属性

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 控件可用时 -->
<item android:drawable="@drawable/enable" android:state_enabled="false"/>
<!-- 当前窗口失去焦点时 -->
<item android:drawable="@drawable/lost_window_focused" android:state_window_focused="false"/>
<!-- 被激活时 -->
<item android:drawable="@drawable/activated" android:state_activated="true" />
<!--获得焦点时-->
<item android:drawable="@drawable/focused" android:state_focused="true"/> <!--被按压时-->
<item android:drawable="@drawable/pressed" android:state_pressed="true"/>
<!--被选中时-->
<item android:drawable="@drawable/selected" android:state_selected="true"/>
<!--默认状态-->
<item android:drawable="@drawable/default"/>
</selector>

使用方式: android:src="@drawable/bg_test"

注意:默认状态需要写在最后。

因为Item是从上往下匹配的,如果匹配到合适的,就不会再往下匹配了,

就是说如果默认状态写在最上方,则只会匹配默认状态,不会往下走。

2.另外除了可以设置背景图片外,还可以设置文本颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--选中状态时-->
<item android:color="@android:color/holo_blue_light" android:state_selected="true"/>
<!--默认状态-->
<item android:color="@android:color/black"/>
</selector>

使用方式: android:textColor="@drawable/text_color"


另外有一点需要注意注意的是,当你想给TextView,ImageView或者Button设置选中时背景图片,需要在控件被点击后,动态设置 btn.setSelected(true); 切记,切记。

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. [JS] selector 背景选择器

    用于listview和button改变android原来控件的背景 android的selector是在drawable/xxx.xml中配置的 1.定义xml 把下面的XML文件保存成你自己命名的. ...

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

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

  8. Android selector背景选择器

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

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

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

  10. Android View 背景选择器编写技巧

    在项目中选择器的使用是非常多的,以下是本人在项目中的一些常用的背景选择器的写法 带边框下划线背景选择器效果图: 上面布局中放了10个CheckBox,然后设置了CheckBox的背景图片位,背景选择器 ...

随机推荐

  1. Python【第四篇】函数、内置函数、递归、装饰器、生成器和迭代器

    一.函数 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 1.定义 def 函数名(参数): ...

  2. golang 笔记

    golang 的坑 package main import ( "fmt" "net/http" "time" ) func Hello(w ...

  3. java java.net.URLConnection 实现http get,post

    抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接.此类的实例可用于读取和写入此 URL 引用的资源.通常,创建一个到 URL 的连接需要几个步骤: open ...

  4. macOS Mojave待机耗电大

    这很有可能是待机时依然链接网络导致的.如果不需要待机时链接网络可以执行 sudo pmset -a tcpkeepalive 0 恢复则执行 sudo pmset -a tcpkeepalive 1

  5. 【洛谷P1134 阶乘问题】

    [传送门] #include<bits/stdc++.h> using namespace std; int main() { ; cin>>a; ;i<=a;i++) ...

  6. 测试框架httpclent 4.HttpClient Post方法实现

    startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求", "reques ...

  7. C# 数独算法——LINQ+委托

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sing ...

  8. 金融量化分析【day111】:Matplotib-画布与子图

    一.画布与子图 1.实例 %matplotlib auto fig = plt.figure() ax = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2 ...

  9. Linux系统在线安装、启动 redis

    环境: Linux系统:VMware 14 中 CentOS 7 64 位 redis版本:redis-stable.tar.gz(当前版本是:redis-cli 4.0.9) 一.Linux在线安装 ...

  10. jQuery使用(十四):extend()方法

    浅层克隆 深层克隆 扩展方法 一.extend的基本使用 语法: $.extend( target [, object1 ] [, objectN ] ) $.extend( [deep ], tar ...