写过android的代码相信大家对Selector并不陌生吧,下面来看看这段xml文件是如何定义的

    <?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 触摸时并且当前窗口处于交互状态 -->
<item android:state_pressed="true" android:state_window_focused="true" android:drawable= "@drawable/pic1" />
<!-- 触摸时并且没有获得焦点状态 -->
<item android:state_pressed="true" android:state_focused="false" android:drawable="@drawable/pic2" />
<!--选中时的图片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic3" />
<!--获得焦点时的图片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic4" />
<!-- 窗口没有处于交互时的背景图片 -->
<item android:drawable="@drawable/pic5" />
</selector>

上面的代码估计很多人都会经常的使用到,除了在xml里面写之外,我们还可以在代码中通过ColorStateList和StateListDrawable来设置Selector,ColorStateList主要是用于颜色的状态,StateListDrawable这个主要是用于设置图片的状态,当然使用这种代码编写的方法我们可以动态设置TextView、Button、ImageView等组件在不同状态下的背景/前景显示效果。而不需要固定死。

package lab.sodino.statelist;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView; /**
* 对TextView设置ColorStateList使其在Normal、Pressed、Focused、Unable四种状态下显示不同的颜色。<br/>
* StateListDrawable可直接使用图片应用在相似场合。
*/
public class ActColorStateList extends Activity implements OnClickListener {
private TextView txtShow; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtShow = (TextView) findViewById(R.id.txtShow);
txtShow.setText("Sodino\nNormal:0xffffffff\nPressed:0xffffff00\nFocused:0xff0000ff\nUnable:0xffff0000");
txtShow.setTextColor(createColorStateList(0xffffffff, 0xffffff00, 0xff0000ff, 0xffff0000));
txtShow.setOnClickListener(this);
} /** 对TextView设置不同状态时其文字颜色。 */
private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
int[][] states = new int[6][];
states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
states[2] = new int[] { android.R.attr.state_enabled };
states[3] = new int[] { android.R.attr.state_focused };
states[4] = new int[] { android.R.attr.state_window_focused };
states[5] = new int[] {};
ColorStateList colorList = new ColorStateList(states, colors);
return colorList;
} /** 设置Selector。 */
public static StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused,
int idUnable) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
// View.PRESSED_ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
// View.ENABLED_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
// View.ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled }, normal);
// View.FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_focused }, focused);
// View.WINDOW_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
// View.EMPTY_STATE_SET
bg.addState(new int[] {}, normal);
return bg;
} @Override
public void onClick(View v) {
if (v == txtShow) {
txtShow.setEnabled(false);
}
}
}

我们看到在上面中出现 states[5] = new int[] {};和bg.addState(new int[] {}, normal);这些就是表示在不是上面任何一种状态的时候默认的值,像上面的这种状态android.R.attr.state_pressed,android.R.attr.state_enabled对应都是在xml中的值都是为true,即android:state_pressed="true"类似这个值,那么如果我这里想要设置为false时可以在前面加上一个 “-”负数的符号,比如bg.addState(new int[] {-android.R.attr.state_focused }, focused);那么这个就是表示state_focused=false时的值为focused。

还有我们可以读取xml中设定的颜色值:比如

 ColorStateList mIdleColorState = getResources().getColorStateList(idleStateSelector);

 int colorNormal=colorStateList.getColorForState(new int[]{android.R.attr.state_enabled}, 0);

 int colorDisabled=colorStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, 0);//“-”负号表示对应的属性值为false

android中ColorStateList及StateListDrawable设置Selector的更多相关文章

  1. 【原创】如何在Android中为TextView动态设置drawableLeft等

    如何在Android中为TextView动态设置drawableLeft等   两种方式:   方式1:手动设置固有边界 Drawable drawable = getResources().getD ...

  2. Android中字体颜色的设置

    1.在Android中经常看到设置的颜色为八位的十六进制的颜色值,例如: 1 2 3 public static final class color {     public static final ...

  3. Android中通过反射来设置Toast的显示时间

    这个Toast的显示在Android中的用途还是非常大的,同一时候我们也知道toast显示的时间是不可控的.我们仅仅能改动他的显示样式和显示的位置,尽管他提供了一个显示时间的设置方法.可是那是没有效果 ...

  4. android中对Bitmap图片设置任意角为圆角

    http://blog.csdn.net/l448288137/article/details/48276681 最近项目开发中使用到了圆角图片,网上找到的圆角图片控件大多比较死板,只可以全圆角.其中 ...

  5. 关于那些Android中不常用的设置属性

    很多在manifest中的属性我们经常遗忘了它们,或者经常看到但又不是很明白它的作用.那么在这里我就拿了一些属性简单的解释一下,防止以后碰到却不知道其中的意思.不是很全,以后会断断续续的补充吧 一.a ...

  6. android中通过代码来设置蓝牙永久可见性

    废话不多说,直接阐述: 前段时间在搞一个android项目,其中有一个功能要求需要蓝牙可见性永久打开,但是开发过android蓝牙的程序员应该都知道,goole提供的api中没有设置蓝牙永久可见性的接 ...

  7. android开发(32) android 中 actionbar 常用方法。设置标题,隐藏图标等

    设置标题: actionBar.setTitle("关于我们"); 使返回箭头出现 actionBar.setDisplayHomeAsUpEnabled(true); 监听返回按 ...

  8. Android中activity背景色的设置

    Android应用开发——系统自带样式Android:theme •android:theme="@android:style/Theme.Dialog"   将一个Activit ...

  9. 【整理】Android中的gravity和layout_gravity区别

    [背景] 在Android中,想要设置个按钮的水平对齐,都累死了: [已解决]ADT中已设置TableLayout布局的情况下如何设置按钮居中对齐    所以现在有必要搞清楚,到底gravity和la ...

随机推荐

  1. SpringBoot Demo

    Spring Boot,微框架,确实不错,很方便. 相关网站链接: http://www.tuicool.com/articles/veUjQba https://www.gitbook.com/bo ...

  2. Extjs MVC开发模式详解

    Extjs MVC开发模式详解   在JS的开发过程中,大规模的JS脚本难以组织和维护,这一直是困扰前端开发人员的头等问题.Extjs为了解决这种问题,在Extjs 4.x版本中引入了MVC开发模式, ...

  3. uva1588kickdown

    题目要求简述:给定长度分别为n1,n2(n1,n2<=100)且每列的高度只为1或者2的长条.需要将他们放入一个高度为3的容器,问能够容纳它们的最短容器长度. 思路就是固定一个字符串a,字符串b ...

  4. Windows 2008 R2 安装 Windows phone 7 开发环境

    安装环境:1.Windows server 2008 R22.Visual Studio 2010 SP1 旗舰版 1.下载 WP7 SDK 离线安装包.(话说要选择与 VS2010 相同语言的版本) ...

  5. C#图片上传服务器缩放存储功能

    项目需求上需要用户上传头像.本来是用第三方的插件的.但是很多都是收费的. 不过,我需要花这钱么?是不?所以网络上搜集了些资料.. 果然.这个世界 大神是很多很多的. 用了个免费插件. <scri ...

  6. OpenVZ VPS加速方案–Final Speed

    body,td { font-family: 微软雅黑; font-size: 10pt }   OpenVZ VPS加速方案–Final Speed OpenVZ VPS加速方案–Final Spe ...

  7. ActiveX: 如何用.inf和.ocx文件生成cab文件

    ActiveX: 如何用.inf和.ocx文件生成cab文件  

  8. SQL Server 的表数据简单操作(表数据查询)

    --表数据查询----数据的基本查询-- --数据简单的查询--select * | 字段名[,字段名2, ...] from 数据表名 [where 条件表达式] 例: use 商品管理数据库 go ...

  9. 记录下最近项目中常用到的SQL语句

    1  实现对字符串的Spilt功能. 比如查出“I have a dream!”总共有几个单词,需要以' '分割,然后再求出总数. ALTER function [dbo].[fc_SpiltStri ...

  10. love easily fade

      Ubuntu addict and loving it , the answerer 's intro attracted me.When I first read a book on linux ...