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针对不同版本进行了封装,更容易使用了,不用我们去管底层实现细节。

我自己写了个工具类,代码如下

  1. package com.sky.support;
  2.  
  3. import android.content.res.ColorStateList;
  4. import android.content.res.TypedArray;
  5. import android.graphics.PorterDuff;
  6. import android.support.v4.view.ViewCompat;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9.  
  10. /**
  11. * Created by sky on 2015/9/15.
  12. * <p/>
  13. * View Utils
  14. */
  15. public class ViewUtils {
  16.  
  17. private static final int[] TINT_ATTRS = {
  18. R.attr.backgroundTint, //in v7
  19. R.attr.backgroundTintMode, //in v7
  20. };
  21.  
  22. public static void supportTint(View view, AttributeSet attrs) {
  23. TypedArray a = view.getContext().obtainStyledAttributes(attrs, TINT_ATTRS);
  24. if (a.hasValue(0)){
  25. //set backgroundTint
  26. ColorStateList colorStateList = a.getColorStateList(0);
  27. ViewCompat.setBackgroundTintList(view, colorStateList);
  28. }
  29. if (a.hasValue(1)){
  30. //set backgroundTintMode
  31. int mode = a.getInt(1, -1);
  32. ViewCompat.setBackgroundTintMode(view, parseTintMode(mode, null));
  33. }
  34. a.recycle();
  35. }
  36.  
  37. /**
  38. * Parses a {@link android.graphics.PorterDuff.Mode} from a tintMode
  39. * attribute's enum value.
  40. *
  41. * @hide
  42. */
  43. public static PorterDuff.Mode parseTintMode(int value, PorterDuff.Mode defaultMode) {
  44. switch (value) {
  45. case 3:
  46. return PorterDuff.Mode.SRC_OVER;
  47. case 5:
  48. return PorterDuff.Mode.SRC_IN;
  49. case 9:
  50. return PorterDuff.Mode.SRC_ATOP;
  51. case 14:
  52. return PorterDuff.Mode.MULTIPLY;
  53. case 15:
  54. return PorterDuff.Mode.SCREEN;
  55. case 16:
  56. return PorterDuff.Mode.ADD;
  57. default:
  58. return defaultMode;
  59. }
  60. }
  61. }

android中如何在低版本(5.0之前)上使用tint(着色)属性的更多相关文章

  1. js中如果遇到低版本安卓设备调用setTimeout不生效解决办法

    工作中会遇到低版本安卓设备调用setTimeout不生效,既不会报错,里面的函数也不会执行,这里po一个解决办法,如果不执行则执行安卓自己封装的原生的setTimeout方法:sdk.setTimeo ...

  2. Windows系统调用中API从3环到0环(上)

    Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html Windows系统调用中API从3环到0环(上) 如果对API在三 ...

  3. 在高版本SDK中打开现存低版本SDK工程

    直接打开低版本SDK工程会出现错误提示:“Unable to resolve target 'android-xx” 解决方法: 1.将project.properties文件中的“target=an ...

  4. C#中npoi操作Excel[版本2.0.1读写2003、2007格式]

    下载npoi2.0.1dll文件:http://download.csdn.net/detail/pukuimin1226/5851747 public static void test1()     ...

  5. android 中遇到 imageView getWidth 始终为0 时 ,设置 setImageBitmap 的方法

    先说说我的遇到的问题: 1. 我在activity里写一个 fragment 2.这个fragment里有个 imageView ,用于显示图片. 我使用 asyncTask获得图片,并准备在这个im ...

  6. LayDate 时间选择插件的使用介绍 (低版本1.0好像是)

    <span style="font-size:18px;"><!doctype html> <html> <head> <me ...

  7. 关于android中出现failed to read row 0,column -1错误

    该错误出现的原因是Cursor.getColumnIndex()的参数列名不存在或者错误,这时返回值为-1.出现该错误

  8. Android中设置半个屏幕大小且居中的button布局 (layout_weight属性)

            先看例如以下布局 : 

  9. Android 进阶Android 中的 IOC 框架 【ViewInject】 (上)

    1.概述 首先我们来吹吹牛,什么叫IoC,控制反转(Inversion of Control,英文缩写为IoC),什么意思呢? 就是你一个类里面需要用到很多个成员变量,传统的写法,你要用这些成员变量, ...

随机推荐

  1. 多个SVG图形集成到一个SVG图形上

    SVG:使用XML格式定义图像的可缩放矢量图形(Scalable Vector Graphics). 优点就不多说了,下面看看怎么将多个svg图形集成到一个svg图形上. 如果使用bootstrap框 ...

  2. Ubuntu16.04安装nginx

    //ubuntu //安装nginxcurl -LJO http://nginx.org/download/nginx-1.10.1.tar.gz tar zxvf nginx-1.10.1.tar. ...

  3. PHP CURL模拟提交数据 攻击N次方

    public function actionCurl(){ $data['DATA']='{"NAME":"c","LEGEND":&quo ...

  4. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  5. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-完整应用例子-在线聊天室系统-下载配置

    一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...

  6. linux 内核与用户空间通信之netlink使用方法

    转自:http://blog.csdn.net/haomcu/article/details/7371835 Linux中的进程间通信机制源自于Unix平台上的进程通信机制.Unix的两大分支AT&a ...

  7. 判断横屏竖屏,然后CSS重新计算

    function hengshuping(){ if(window.orientation==180||window.orientation==0){ alert("竖屏状态!") ...

  8. python 异常

    引用一段来自菜鸟教程的文章:http://www.runoob.com/python/python-exceptions.html Python 异常处理 python提供了两个非常重要的功能来处理p ...

  9. Github上安卓榜排名第2的程序员教你如何学习【转载,侵删】

    来自:峰瑞资本(微信号:freesvc)文章作者:代码家(微信 ID:daimajia_share) 软件早已吞噬整个世界,程序员是关键角色.过去 40 年中,许多伟大的公司都由程序员缔造,比如比尔· ...

  10. OSI七层模型及TCP/IP四层模型

    1)  OSI七层模型及TCP/IP四层模型 OSI七层模型:是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系.它是一个七层的.抽象的模型,不仅包括一系列抽象的术语或概念,也包 ...