众所周知,android可以通过XML文件来创建selector,以Drawable对象的形式安装到组件上,以提供统一的风格设置。但是在某些时候,我们需要通过代码的形式来实现相同的功能,例如组件数量非常多,对应不同的图片,这时候如果还用XML的话就需要创建大量的selector文件,非常繁琐。

例如一个TextView使用了如下的selector

<TextView
android:id="@+id/TextView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:drawableTop="@drawable/selector_tabwidget_icon"
android:textAlignment="center"
/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact_sel" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" />
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/contact_sel" />
<item android:state_pressed="true" android:drawable="@drawable/contact_sel" />
</selector>

 里面所引用的图片资源文件非常多,如果每个文件都对应一个XML的文件的话,就会非常繁琐,修改起来非常麻烦。

  实际上,所有XML设定能做的事情,android里同样可以用编码的方式来实现,像上面那个XML文件,就可以就下面的代码来实现:

StateListDrawable drawable = new StateListDrawable();
//Non focused states
drawable.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.contact));
drawable.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.contact_sel));
//Focused states
drawable.addState(new int[]{android.R.attr.state_focused,-android.R.attr.state_selected, -android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.contact_sel));
drawable.addState(new int[]{android.R.attr.state_focused,android.R.attr.state_selected, -android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.contact_sel));
//Pressed
drawable.addState(new int[]{android.R.attr.state_selected, android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.contact_sel));
drawable.addState(new int[]{android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.contact_sel)); TextView textView = (TextView) findViewById(R.id.TextView_title); textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);

注意里面的“-”号,当XML的设定是false时,就需要使用资源符号的负值来设定。

Android 用代码来实现selector的更多相关文章

  1. Android菜鸟成长记9 -- selector的用法

    在项目开发的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片.开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点 ...

  2. Android实用代码模块集锦

    1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx)  ...

  3. Android RadioGroup 及资源文件 & selector

    RadioGroup :单选组         RadioButton :单选按钮 RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单 ...

  4. Android:自定义控件样式(Selector)

    前言 在开发一个应用程序过程中不可避免的要去修改组件的样式,比如按钮.输入框等.现在就看下如何通过Seletor实现样式的自定义.先看下简单的效果对比

  5. Android 实用代码七段(一)

    前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...

  6. Android实用代码七段(一)

    前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...

  7. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

  8. Intellij idea 和android studio 代码给混淆

    Intellij idea 和android studio 代码给混淆 一.指令说明-optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassna ...

  9. Android实用代码七段(五)

      前言  每次分享意味着每次都有进步,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯 ...

随机推荐

  1. 洛谷 P1731 生日蛋糕

    /*洛谷 1731 生日蛋糕 傻傻的-1 T成了傻逼*/ #include<cstdio> #include<iostream> #include<cmath> # ...

  2. 多维数组遍历PHP

    原文出处 <?php /* * ------------------------------------------------- * Author : nowamagic * Url : ww ...

  3. Spring JdbcTemplate批量操作数据库

    个人总结,转载请注明出处:http://www.cnblogs.com/lidabnu/p/5769732.html 还是分两部分:解决什么问题和怎么做. 解决什么问题 提升数据操作性能,因为批量操作 ...

  4. 1. 连接字符串的创建 - Lazy.Framework从零开始设计自己的ORM架构

    开发初衷 注册了博客园已经有几个月了,却从来都没有上来过,本人大概从2010年开始就开始做.NET 方向的开发. 这个是我在博客园发布的第一个帖子. 主要就是说说最近在写的一个ORM架构. 本人接触的 ...

  5. Nico Game Studio 3.地图纹理编辑 物体皮肤编辑

    完成功能: 1.地图纹理编辑功能. 图层编辑,添加/删除纹理,地图编辑.网格绘制.

  6. IE6 中的最大最小寬度和高度 css 高度 控制(兼容版本)

    /* 最小寬度 */.min_width{min-width:300px; /* sets max-width for IE */ _width:expression(document.body.cl ...

  7. (转)asp.net基础-HttpModule

    HttpModule是向实现类提供模块初始化和处置事件.当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于H ...

  8. SQL语句一之建库

    USE master --转到系统表goIF EXISTS(SELECT *  FROM sysdatabases WHERE name ='Test') --查询是否存在Test数据库DROP DA ...

  9. .NET(C#):获取进程的CPU使用状况

    第一个是通过手动的方法来计算CPU使用比例:CPU使用比例 = 在间隔时间内进程的CPU使用时间 除以 计算机逻辑CPU数量. 使用Process类的UserProcessorTime和Privile ...

  10. UITextField 对输入金额的约束

    [2016/1/18更新] -- 五个人辛辛苦苦干了一年的项目终于上线了,今天有空看了一下正则表达式教程,然后开始rebuild之前的种种对字符串的约束,首先就从这个金额输入框开始吧,修改后的代码如下 ...