当我们设置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. [OJ] Data Stream Median (Hard)

    LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...

  2. WCF - Windows Service Hosting

    WCF - Windows Service Hosting The operation of Windows service hosting is a simple one. Given below ...

  3. NoClassDefFoundError: javassist/util/proxy/MethodFilter

    Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter    at org.hibernate.byt ...

  4. Running an etcd cluster on localhost

    Purpose Run a cluster on localhost while investigating etcd Use a static cluster (So we have no exte ...

  5. 屯题50AC纪念

    从2.1起开始屯题,一直弄到现在才完成了一发50题的目标,实在太弱 (当然之间事比较多,还是挺不容易的) 不过总算是完成了一个小的目标了 接下来两周要进行小高考最后冲刺了,所以我大概不会再怎么刷题了 ...

  6. 【转】为Xcode添加删除行、复制行快捷键

    原文网址:http://www.jianshu.com/p/cc6e13365b7e 在使用eclipse过程中,特喜欢删除一行和复制一行的的快捷键.而恰巧Xcode不支持这两个快捷键,再一次的恰巧让 ...

  7. Servlet能读到JSessionID,读不到其它cookie问题

    Servlet的Cookie值保存与获取 今天测试设置和获取Cookie遇到了一点小问题,很奇怪的问题: 把J2ee服务部署在本地 8080端口:访问任何一个服务时,如果客户端没有cookie,则下发 ...

  8. WFS

    Web 要素服务(WFS) 1定义 支持对地理要素的插入,更新,删除,检索和发现服务.该服务根据HTTP客户请求返回GML(Geography Markup Language.地理标识语言)数据. W ...

  9. MySql配置参数很全的Mysql配置参数说明

    MySql配置参数 很全的Mysql配置参数说明 1. back_log 指定MySQL可能的连接数量.当MySQL主线程在很短的时间内得到非常多的连接请求,该参数就起作用,之后主线程花些时间(尽管很 ...

  10. powershell学习

    PowerShell 调试器 在开始运行处,输入powershell ISE回车即可 PowerShell 与操作系统版本 powershell在windows server 2008上自带,但最好在 ...