1、资源解析成Drawable

  getDrawable(int id); 挺简单一方法,可是 require api 21......如何向下兼容呢????

  幸亏有ContextCompat类。。。(接触过类似的类有:ViewCompat)

  ContextCompat.getDrawable();

2、TextView 设置Drawable

     简单!

  tv_message.setCompoundDrawables(d,null,null,null);

  但是,光这一句没什么效果,发现,图片没了。。。。。怎么回事!

  还是度娘吧!

  d.setBounds(0,0,d.getMinimumWidth(),d.getMinimumHeight());

  加上这一句就搞定了!

3、Drawable着色问题

DrawableCompat.setTint(tv_home.getCompoundDrawables()[0], ContextCompat.getColor(getContext(),R.color.colorToolbar));

  发现在低版本手机上没有效果,但是在6.0之上就可以顺利着色,为什么呢?

  查看代码发现

   public static void setTint(Drawable drawable, int tint) {
        if (drawable instanceof TintAwareDrawable) {
            ((TintAwareDrawable) drawable).setTint(tint);
        }
    }

  原来需要实现这个类,怪不得呢,那怎么办呢?总不见得自己去实现吧。。。。

  幸好还有个方法wrap() 包装一层,最后会调用

  public static Drawable wrapForTinting(Drawable drawable) {
        if (!(drawable instanceof TintAwareDrawable)) {
            return new DrawableWrapperHoneycomb(drawable);
        }
        return drawable;
    }

这样就顺利解决啦

   DrawableCompat.setTint(DrawableCompat.wrap(tv_home.getCompoundDrawables()[0]), ContextCompat.getColor(getContext(),R.color.colorToolbar));

 

posted on 2016-10-19 15:24  向着大牛奋斗  阅读(384)  评论(0编辑  收藏  举报