设置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 ...
随机推荐
- JQuery实现点击div以外的位置隐藏该div窗口
简单示例代码: <body> <script type="text/javascript" src="http://ajax.googleapis.co ...
- Configurataion Printer(基于全新2.2.0API)
Configurataion Printer import java.util.Map.Entry; import org.apache.hadoop.conf.Configuration; impo ...
- linux 失败无连接 检查电缆吗
将BOOTPROTO=dhcp改成 BOOTPROTO=static 改成手动获取IP的模式 原因: 虚拟机中的Linux目前是默认设成的自动获取IP设置,但你的网络中没有DHCP服务,所以会显示“正 ...
- 修饰模式(Decorator结构型)C#简单例子
修饰模式(Decorator结构型)C#简单例子 玩家基本功能是移动.运行等等.BaseAbility新增加功能:1.伤害技能harmAbility:2.阻碍技能BaulkAbility:3.辅助技能 ...
- Linux kernel ‘aac_send_raw_srb’函数输入验证漏洞
漏洞名称: Linux kernel ‘aac_send_raw_srb’函数输入验证漏洞 CNNVD编号: CNNVD-201311-422 发布时间: 2013-11-29 更新时间: 2013- ...
- SCOI2007排列perm
1072: [SCOI2007]排列perm Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 805 Solved: 497[Submit][Stat ...
- ☀【JS】Code
1 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf- ...
- 宣布发布长期保留 Azure Backup功能
Shreesh Dubey 云 + Enterprise首席项目经理 此前我们已宣布为DPM云备份提供长期保留功能.随着本月 Azure Backup 服务的发布,我们将此功能扩展到云备份目前支持 ...
- 创建通用型framework
http://years.im/Home/Article/detail/id/52.html http://www.cocoachina.com/industry/20131204/7468.html ...
- ARM-Linux配置DHCP自动获取IP地址
备注:内核版本:2.6.30.9busybox版本:1.15.2 PC Linux和开发板Linux的工作用户:root 1. 配置内核:[*] Networking support --->N ...