如何将可绘制集的颜色更改为 android:background?
How to change color of drawable set as android:background?
我的约束布局中有一个如下所示的图标:icon。
这是我的布局 xml 此图像视图:
<ImageView
android:id="@+id/vTotalPaidAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@null"
android:src="@drawable/ic_group_ic_total"
tools:src="@drawable/ic_group_ic_total"
app:layout_constraintLeft_toLeftOf="@+id/vSettleDebtsLayout"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="@+id/vSettleDebtsLayout"
android:layout_marginTop="16dp"
android:background="@drawable/avatar_circle" />
如您所见,该图标只是一个美元符号。我将那个灰色圆圈设置为背景。问题是我需要动态更改该背景的颜色。美元符号需要保持白色。
我的问题是,有什么方法可以只改变背景颜色吗?
另一种解决方案是将该圆圈用作下一个图像视图,然后更改该图像视图的颜色。但我认为可能会有一些更“明确的解决方案来解决这个问题。
感谢提示:)
您尝试 android:background="@color/my_color"
并通过代码 imageView.setBackgroundColor(Color.rgb(255, 255, 255));
了吗?
你一定要试试
android:backgroundTint="your color"
在图像视图标签中
这样就可以了
尝试:
yourImage.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
这实际上是从 ImageView
获得 背景 (没有 src
)并用颜色覆盖其形状。 drawable 和给定颜色的组合方式由 Porter Duff 的模式定义 - here 你有更多关于它的信息。
最佳方法:
public static void colorDrawable(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
if (wrappedDrawable != null) {
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
setBackgroundDrawable(view, wrappedDrawable);
}
}
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}
我的约束布局中有一个如下所示的图标:icon。
这是我的布局 xml 此图像视图:
<ImageView
android:id="@+id/vTotalPaidAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@null"
android:src="@drawable/ic_group_ic_total"
tools:src="@drawable/ic_group_ic_total"
app:layout_constraintLeft_toLeftOf="@+id/vSettleDebtsLayout"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="@+id/vSettleDebtsLayout"
android:layout_marginTop="16dp"
android:background="@drawable/avatar_circle" />
如您所见,该图标只是一个美元符号。我将那个灰色圆圈设置为背景。问题是我需要动态更改该背景的颜色。美元符号需要保持白色。 我的问题是,有什么方法可以只改变背景颜色吗?
另一种解决方案是将该圆圈用作下一个图像视图,然后更改该图像视图的颜色。但我认为可能会有一些更“明确的解决方案来解决这个问题。
感谢提示:)
您尝试 android:background="@color/my_color"
并通过代码 imageView.setBackgroundColor(Color.rgb(255, 255, 255));
了吗?
你一定要试试
android:backgroundTint="your color"
在图像视图标签中
这样就可以了
尝试:
yourImage.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
这实际上是从 ImageView
获得 背景 (没有 src
)并用颜色覆盖其形状。 drawable 和给定颜色的组合方式由 Porter Duff 的模式定义 - here 你有更多关于它的信息。
最佳方法:
public static void colorDrawable(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
if (wrappedDrawable != null) {
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
setBackgroundDrawable(view, wrappedDrawable);
}
}
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}