使 GradientDrawable 无效

Invalidate GradientDrawable

我有以下 xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">

    <solid android:color="@color/color_primary_dark" />
    <size android:width="25dp" android:height="25dp" />
    <stroke android:width="1dp" android:color="@android:color/white" />

</shape>

在我的应用程序中的某个时刻,我像这样更改了这个形状的颜色:

// priority_button has this shape as is src
(priority_button.drawable as GradientDrawable).setColor(ContextCompat.getColor(this, android.R.color.holo_orange_light))

问题是:如果我销毁 activity 然后重新打开相同的 activity,这个形状会保留我最后设置的颜色(在本例中 android.R.color.holo_orange_light ) 我想将该形状重置为 xml (R.color.color_primary_dark).

中定义的相同颜色

因为我以编程方式设置颜色,所以我认为我正在更改 xml 本身,当 activity 重新打开并绘制视图时,它得到 xml 我设置了另一种颜色。

有什么方法可以重置 xml 值或类似的东西吗?

尝试在 drawable 上调用 mutate(),然后再更改其颜色。

根据 setColor(int argb) 文档,它指出:

changing color will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing the color.

所以也许这也会影响到你。