android中如何在低版本(5.0之前)上使用tint(着色)属性
1. 使用app前缀(app:backgroundTint,app:backgroundTintMode),如果使用android前缀,在低版本上是拿不到值的,因为这些属性是5.0以后才加入的。
2. 自定义ATTRS数组,使用obtainStyledAttributes方法得到app:backgroundTint和app:backgroundTintMode的值。
3. 使用v4包中的ViewCompat类----ViewCompat.setBackgroundTintList,ViewCompat.setBackgroundTintMode方法设置tint。
大功告成,这样可以同时兼容4.x和5.x。如果只在java代码中使用,其实只要使用ViewCompat类就好了,其实底层原理是一样的,都是ColorFilter、PorterDuff在起作用,ViewCompact针对不同版本进行了封装,更容易使用了,不用我们去管底层实现细节。
我自己写了个工具类,代码如下
- package com.sky.support;
- import android.content.res.ColorStateList;
- import android.content.res.TypedArray;
- import android.graphics.PorterDuff;
- import android.support.v4.view.ViewCompat;
- import android.util.AttributeSet;
- import android.view.View;
- /**
- * Created by sky on 2015/9/15.
- * <p/>
- * View Utils
- */
- public class ViewUtils {
- private static final int[] TINT_ATTRS = {
- R.attr.backgroundTint, //in v7
- R.attr.backgroundTintMode, //in v7
- };
- public static void supportTint(View view, AttributeSet attrs) {
- TypedArray a = view.getContext().obtainStyledAttributes(attrs, TINT_ATTRS);
- if (a.hasValue(0)){
- //set backgroundTint
- ColorStateList colorStateList = a.getColorStateList(0);
- ViewCompat.setBackgroundTintList(view, colorStateList);
- }
- if (a.hasValue(1)){
- //set backgroundTintMode
- int mode = a.getInt(1, -1);
- ViewCompat.setBackgroundTintMode(view, parseTintMode(mode, null));
- }
- a.recycle();
- }
- /**
- * Parses a {@link android.graphics.PorterDuff.Mode} from a tintMode
- * attribute's enum value.
- *
- * @hide
- */
- public static PorterDuff.Mode parseTintMode(int value, PorterDuff.Mode defaultMode) {
- switch (value) {
- case 3:
- return PorterDuff.Mode.SRC_OVER;
- case 5:
- return PorterDuff.Mode.SRC_IN;
- case 9:
- return PorterDuff.Mode.SRC_ATOP;
- case 14:
- return PorterDuff.Mode.MULTIPLY;
- case 15:
- return PorterDuff.Mode.SCREEN;
- case 16:
- return PorterDuff.Mode.ADD;
- default:
- return defaultMode;
- }
- }
- }
android中如何在低版本(5.0之前)上使用tint(着色)属性的更多相关文章
- js中如果遇到低版本安卓设备调用setTimeout不生效解决办法
工作中会遇到低版本安卓设备调用setTimeout不生效,既不会报错,里面的函数也不会执行,这里po一个解决办法,如果不执行则执行安卓自己封装的原生的setTimeout方法:sdk.setTimeo ...
- Windows系统调用中API从3环到0环(上)
Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html Windows系统调用中API从3环到0环(上) 如果对API在三 ...
- 在高版本SDK中打开现存低版本SDK工程
直接打开低版本SDK工程会出现错误提示:“Unable to resolve target 'android-xx” 解决方法: 1.将project.properties文件中的“target=an ...
- C#中npoi操作Excel[版本2.0.1读写2003、2007格式]
下载npoi2.0.1dll文件:http://download.csdn.net/detail/pukuimin1226/5851747 public static void test1() ...
- android 中遇到 imageView getWidth 始终为0 时 ,设置 setImageBitmap 的方法
先说说我的遇到的问题: 1. 我在activity里写一个 fragment 2.这个fragment里有个 imageView ,用于显示图片. 我使用 asyncTask获得图片,并准备在这个im ...
- LayDate 时间选择插件的使用介绍 (低版本1.0好像是)
<span style="font-size:18px;"><!doctype html> <html> <head> <me ...
- 关于android中出现failed to read row 0,column -1错误
该错误出现的原因是Cursor.getColumnIndex()的参数列名不存在或者错误,这时返回值为-1.出现该错误
- Android中设置半个屏幕大小且居中的button布局 (layout_weight属性)
先看例如以下布局 :
- Android 进阶Android 中的 IOC 框架 【ViewInject】 (上)
1.概述 首先我们来吹吹牛,什么叫IoC,控制反转(Inversion of Control,英文缩写为IoC),什么意思呢? 就是你一个类里面需要用到很多个成员变量,传统的写法,你要用这些成员变量, ...
随机推荐
- 多个SVG图形集成到一个SVG图形上
SVG:使用XML格式定义图像的可缩放矢量图形(Scalable Vector Graphics). 优点就不多说了,下面看看怎么将多个svg图形集成到一个svg图形上. 如果使用bootstrap框 ...
- Ubuntu16.04安装nginx
//ubuntu //安装nginxcurl -LJO http://nginx.org/download/nginx-1.10.1.tar.gz tar zxvf nginx-1.10.1.tar. ...
- PHP CURL模拟提交数据 攻击N次方
public function actionCurl(){ $data['DATA']='{"NAME":"c","LEGEND":&quo ...
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-完整应用例子-在线聊天室系统-下载配置
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- linux 内核与用户空间通信之netlink使用方法
转自:http://blog.csdn.net/haomcu/article/details/7371835 Linux中的进程间通信机制源自于Unix平台上的进程通信机制.Unix的两大分支AT&a ...
- 判断横屏竖屏,然后CSS重新计算
function hengshuping(){ if(window.orientation==180||window.orientation==0){ alert("竖屏状态!") ...
- python 异常
引用一段来自菜鸟教程的文章:http://www.runoob.com/python/python-exceptions.html Python 异常处理 python提供了两个非常重要的功能来处理p ...
- Github上安卓榜排名第2的程序员教你如何学习【转载,侵删】
来自:峰瑞资本(微信号:freesvc)文章作者:代码家(微信 ID:daimajia_share) 软件早已吞噬整个世界,程序员是关键角色.过去 40 年中,许多伟大的公司都由程序员缔造,比如比尔· ...
- OSI七层模型及TCP/IP四层模型
1) OSI七层模型及TCP/IP四层模型 OSI七层模型:是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系.它是一个七层的.抽象的模型,不仅包括一系列抽象的术语或概念,也包 ...