0

我在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 模式更改为一些替代方案,但圆圈继续呈现白色。

任何帮助/指导将不胜感激。

4

1 回答 1

2

不要试图修改当前的 xml 可绘制对象,而是在您的代码中创建可绘制对象。如果不需要渐变,也可以使用ShapeDrawable 。

ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(newColor);
于 2013-04-15T04:30:29.103 回答