Tint(着色器)的兼容实现

我们在做控件的时候可以使用tint这个属性给控件上色,这样在很多情况下能减少不同色彩的资源色彩。在MD设计中,控件颜色随着主体改变也是一大要点,今天我们就来看看如何使用它。

使用步骤:1.建立一个控件;2.使用ViewCompat.setBackgroundTintList

      // 第一种实现
        ColorStateList cl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
        ViewCompat.setBackgroundTintList(pasteEt, cl);
        // 第二种实现
        ViewCompat.setBackgroundTintList(pasteEt, getResources().getColorStateList(R.color.red));

这里的colorstateList是什么东西呢,如果做过属性定制的同学一定见过它,它就是一个color的xml文件,这里的color我用了最简单的定义方式:

<color name="red">#ff0000</color>

我们有时候还会像selector一样定义不同状态下的颜色,最终这些xml文件最终解析转为的东西就是colorStateList对象啦,这个对象保存了状态和不同状态下的演示值。

 

 

参考自:

http://segmentfault.com/a/1190000003038675?utm_source=Weibo&utm_medium=shareLink&utm_campaign=socialShare

http://stackoverflow.com/questions/27735890/lollipops-backgroundtint-has-no-effect-on-a-button

posted @ 2015-08-03 12:19  developer_Kale  阅读(2378)  评论(0编辑  收藏  举报
网站流量统计工具