设置background属性使用selector的时候内置?attr报错的解决方案
当我们设置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报错的解决方案的更多相关文章
- Idea中JSP页面中out内置对象报错out.println标红问题
问题如图: 解决方法: 导入jar包 1.在pom.xml的<dependencies>里面复制 <dependency> <groupId>javax.servl ...
- 多态,封装,反射,类内置attr属性,os操作复习
1.多态 #多态 多态是指对象如何通过他们共同的属性和动作来操作及访问,而不需要考虑他们具体的类 运行时候,多种实现 反应运行时候状态 class H2O: def __init__(self,nam ...
- python - 类的内置 attr 方法
类的内置 attr 方法 #类的内置 attr 方法: # __getattr__ # __setattr__ # __delattr__ # __getattr__ #到调用一个类不存在数参数时,将 ...
- Py修行路 python基础 (十八) 反射 内置attr 包装
一.isinstance 和 issubclass1.isinstance(obj,cls)检查是否obj是否是类 cls 的对象.2.issubclass(sub, super)检查sub类是否是 ...
- Spring Boot 添加jersey-mvc-freemarker依赖后内置tomcat启动不了解决方案
我在我的Spring Boot 项目的pom.xml中添加了jersey-mvc-freemarker依赖后,内置tomcat启动不了. 报错信息如下: org.springframework.con ...
- css中设置background属性
属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: backgrou ...
- 如何设置IntelliJ IDEA智能感知支持Jsp内置对象
一.问题: 在IntelliJ IDEA 新建jsp页面,想通过内置对象获取查询参数,比如:request.getParameter("id"),虽然手动输入后能够运行成功,但是智 ...
- javaScript数组的三种属性—数组索引、数组内置属性、数组自定义属性
JS数组也是一种对象. 我们用JavaScript处理的所有东西,都可以视为是一种对象. JavaScript中有两种数据类型,基本类型数对象类型,但是基本类型基本都是包括在对象类型之中的. 数组.函 ...
- Day 20: 面向对象【多态,封装,反射】字符串模块导入/内置attr /包装 /授权
面向对象,多态: 有时一个对象会有多种表现形式,比如网站页面有个按钮, 这个按钮的设计可以不一样(单选框.多选框.圆角的点击按钮.直角的点击按钮等),尽管长的不一样,但它们都有一个共同调用方式,就是o ...
随机推荐
- Maprduce重写参考
Maprduce数据流走向图: 流程解释 Input files 功能描述:存储在HDFS中的文件数据 InputFormat 功能描述:1 ...
- Linux Kernel 本地内存损坏漏洞
漏洞名称: Linux Kernel 本地内存损坏漏洞 CNNVD编号: CNNVD-201310-663 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等级: 漏洞类 ...
- Setting Up the ADT Bundle
Setting Up the ADT Bundle The ADT Bundle provides everything you need to start developing apps, incl ...
- MVVM Light中的Message
比喻:像漂流瓶一样发送一个Message,任何人有兴趣就可以拾起来. MVVM Light中的Message的使用分为三个步骤: 1.创建一个类,包含要传递的Message. 2.在ViewModel ...
- UVa 11752 - The Super Powers 数学
请看这个说明http://blog.csdn.net/u014800748/article/details/45914353 #define _CRT_SECURE_NO_WARNINGS #incl ...
- “System.Exception”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理
“System.Exception”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理 其他信息: OCIEnvCreate 失败,返回代码为 -,但错误消息文本不可用. 如 ...
- SR4K的API使用(libMesaSR.dll)
看看libMesaSR.dll的导出函数: 遇到的问题: error C4430: 缺少类型说明符 - 假定为 int.注意: C++ 不支持默认 int 解决: HWND等未定义 解决: #incl ...
- OpenGL ES 的三种变量类型(uniform,attribute和varying)
1.uniform变量 uniform变量是外部application程序传递给(vertex和fragment)shader的变量.因此它是application通过函数glUniform**()函 ...
- [TVYJ1096]数字组合
时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 在N个数中找出其和为M的若干个数.先读入正整数N(1<N<100)和M(1<M<100 ...
- leetcode—Populating Next Right Pointers in Each Node
1.题目描述 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLink ...