我在xml中定义了一个白色背景的可绘制椭圆。我将这个圆圈设置为textView
我的 gridAdapter 内部的背景资源。
我想通过代码更改可绘制圆圈的颜色,但运气不佳。我用过.setColor
,.setColorFilter
和其他各种黑客,但没有一个奏效。圆圈在背景中加载textView
为白色,它似乎永远不会改变。
这里我尝试加载drawable并修改它的颜色,然后将它作为背景资源添加到textView
:
int color = Color.parseColor(locationModel.col_line_color); // Color of the line
GradientDrawable bgcircle = (GradientDrawable) gridContext.getResources().getDrawable(R.drawable.circle);
bgcircle.mutate();
// bgcircle.setColor(color);
bgcircle.setColorFilter(color, Mode.SRC_ATOP);
viewHolder.textView.setText((locationModel.col_line).toString());
viewHolder.textView.setBackgroundResource(R.drawable.circle);
可绘制形状的 XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@android:color/white"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
很多教程都要求.mutate()
then 实现.setColorFilter()
,但我运气不佳。我尝试将 PorterDuff 模式更改为一些替代方案,但圆圈继续呈现白色。
任何帮助/指导将不胜感激。