当我们设置background属性的时候可以设置background="@color/black"

也可以设置 background="@drawable/selector"

然后selector我们可以这样设置

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 没有焦点时的背景图片 -->
<item android:drawable="@color/colorPressed" android:state_pressed="true"/>
<item android:drawable="@color/colorPressed" android:state_focused="true"/>
<item android:drawable="@color/colorPressed" android:state_selected="true"/>
<item android:drawable="@color/black" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
</selector>
但是当我们设置了属性来设置这个颜色的时候就会遇到崩溃
比如我们这样
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 没有焦点时的背景图片 -->
<item android:drawable="@color/colorPressed" android:state_pressed="true"/>
<item android:drawable="@color/colorPressed" android:state_focused="true"/>
<item android:drawable="@color/colorPressed" android:state_selected="true"/>
<item android:drawable="?attr/backgroundColorValue" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
</selector>
运行的话会直接崩溃,报错的原因是无法获取到对应的drawable对象,至于为什么无法将颜色转换成对应的drawable我也不清楚,既然不能转换那我们可以手动转换
解决方案:
手动创建一个shape.xml,如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="?attr/backgroundColorValue" />
</shape>
然后我们再到selector中引用这个drawable就可以了
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 没有焦点时的背景图片 -->
<item android:drawable="@color/colorPressed" android:state_pressed="true"/>
<item android:drawable="@color/colorPressed" android:state_focused="true"/>
<item android:drawable="@color/colorPressed" android:state_selected="true"/>
<item android:drawable="@drawable/shape" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
</selector>
2016-11-11补充 :
这种方式在android6.0以上设备可用,但在android5.0及以下设备仍然会崩溃
这个时候将 shape中的
<solid android:color="?attr/backgroundColorValue" />
修改为
<solid android:background="?attr/backgroundColorValue" />
就不会崩溃,但在android6.0设备上背景颜色就变成了透明,而在5.0以下设备就没有问题
因此这不是一个好的解决方案,很多设备没有升级到6.0
参考 http://blog.csdn.net/qingfeng812/article/details/51601500 

经过一番查找,目前已经了解到根本的原因,出现这个问题,主要是因为android在api23版本才支持selector从主题中读取颜色值
低于api23只能使用代码去支持,所以我们需要用代码去创建这个selector,然后才可以实现对应的切换
参考 http://blog.csdn.net/qq284565035/article/details/52177225 至于如何用代码创建selector,参见如下代码:
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);

参考 https://my.oschina.net/non6/blog/298156

http://www.cnblogs.com/jason-star/archive/2012/09/28/2706698.html

设置background属性使用selector的时候内置?attr报错的解决方案的更多相关文章

  1. Idea中JSP页面中out内置对象报错out.println标红问题

    问题如图: 解决方法: 导入jar包 1.在pom.xml的<dependencies>里面复制 <dependency> <groupId>javax.servl ...

  2. 多态,封装,反射,类内置attr属性,os操作复习

    1.多态 #多态 多态是指对象如何通过他们共同的属性和动作来操作及访问,而不需要考虑他们具体的类 运行时候,多种实现 反应运行时候状态 class H2O: def __init__(self,nam ...

  3. python - 类的内置 attr 方法

    类的内置 attr 方法 #类的内置 attr 方法: # __getattr__ # __setattr__ # __delattr__ # __getattr__ #到调用一个类不存在数参数时,将 ...

  4. Py修行路 python基础 (十八) 反射 内置attr 包装

    一.isinstance 和 issubclass1.isinstance(obj,cls)检查是否obj是否是类 cls 的对象.2.issubclass(sub, super)检查sub类是否是 ...

  5. Spring Boot 添加jersey-mvc-freemarker依赖后内置tomcat启动不了解决方案

    我在我的Spring Boot 项目的pom.xml中添加了jersey-mvc-freemarker依赖后,内置tomcat启动不了. 报错信息如下: org.springframework.con ...

  6. css中设置background属性

    属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: backgrou ...

  7. 如何设置IntelliJ IDEA智能感知支持Jsp内置对象

    一.问题: 在IntelliJ IDEA 新建jsp页面,想通过内置对象获取查询参数,比如:request.getParameter("id"),虽然手动输入后能够运行成功,但是智 ...

  8. javaScript数组的三种属性—数组索引、数组内置属性、数组自定义属性

    JS数组也是一种对象. 我们用JavaScript处理的所有东西,都可以视为是一种对象. JavaScript中有两种数据类型,基本类型数对象类型,但是基本类型基本都是包括在对象类型之中的. 数组.函 ...

  9. Day 20: 面向对象【多态,封装,反射】字符串模块导入/内置attr /包装 /授权

    面向对象,多态: 有时一个对象会有多种表现形式,比如网站页面有个按钮, 这个按钮的设计可以不一样(单选框.多选框.圆角的点击按钮.直角的点击按钮等),尽管长的不一样,但它们都有一个共同调用方式,就是o ...

随机推荐

  1. maven 简单实用教程

    1. Maven介绍 1.1. 简介 java编写的用于构建系统的自动化工具. 目前版本是2.0.9,注意maven2和maven1有很大区别,阅读第三方文档时需要区分版本. 1.2. Maven资源 ...

  2. 结构体dict_index_t;

    /** InnoDB B-tree index */ typedef struct dict_index_struct dict_index_t; /** Data structure for an ...

  3. VM上成功安装mac os x

    想必很多人在intel . AMD上安装苹果系统,总会遇到很多问题,今天我就将我成功安装的过程给大家分享一下. 下面是我在VM上安装的步骤: 1. 准备软件 OS X Snow Leopard 10. ...

  4. 【转】 Xcode基本操作 -- 不错

    原文网址:http://blog.csdn.net/phunxm/article/details/17044337 1.Xcode IDE概览 说明:从左到右,依次是“导航窗格(Navigator)- ...

  5. [Tommas] SQL 中 WITH AS 的用法

    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到: 下面的例子定义了一个 Temp 片段,Te ...

  6. 执行sql update use c#

    今天犯了个大错 public static void ChangeGoodsCounts(int GoodsID, int changCounts) { int lastCount; using (S ...

  7. 远程连接centos

    yum install tigervnc   yum install tigervnc-server Windows 7下载客户端 tigervnc-1.2.0.exe,在http://sourcef ...

  8. bzoj 1878 [SDOI2009]HH的项链(离线处理+BIT)

    Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...

  9. 使用Windows Azure创建Linux系统虚拟机-下

    如何将数据磁盘附加到新虚拟机 您的应用程序可能需要存储数据.要这样设置,您可以将数据磁盘添加到先前创建的虚拟机.要做到这一点,最简单的方法是将空数据磁盘连接到本机. 在Linux上,磁盘资源通常由Az ...

  10. SPI协议及其工作原理浅析

    转载自:http://bbs.chinaunix.net/thread-1916003-1-1.html一.概述. SPI, Serial Perripheral Interface, 串行外围设备接 ...