以编程方式更改矢量布局的填充颜色或strokeColor

时间:2016-12-01 21:56:51

标签: java android

使用编程方式更改笔触或填充颜色。是否可以以编程方式更改矢量的填充颜色,而不是笔触颜色。

  

vector_icon.xml

null

我使用矢量drawable的图像视图。

Object.__proto__.__proto__.__proto__ === null

我试图将VectorDrawable强制转换为GradientDrawable,但它会抛出类强制转换execption。

var observerConfig = {childList: true}, // only notify when children added
    targets = {
      test: false,
      ...other <div>s to observe
    },
    target, elem, observer;

for (var target in targets) {
  elem = document.getElementById(target);

  // create and activate an observer instance
  observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      // here we assume that the only changes are children addition
      // so we don't look at anything more precise
      targets[elem] = true;
    });
    observer.observe(elem, observerConfig);
  });
}

// then you can test targets['someTarget'] to know if it had been populated
  

java.lang.ClassCastException:android.graphics.drawable.VectorDrawable无法强制转换为android.graphics.drawable.LayerDrawable

1 个答案:

答案 0 :(得分:0)

你在这里没有很多选择,你只需要接受ivFavourite.getDrawable();返回VectorDrawable的事实,它不允许你通过代码更改Storke。

您可以查看此课程的完整文档here

一种可能的解决方法是从其他xml获取VectorDrawable并通过调用ivFavourite.setBackgroundDrawable(myVectorDrawable)

覆盖existen。