有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将可绘制形状的颜色更改为给定的十六进制

我有以编程方式创建的textView。它们需要有不同的颜色值。我不需要动画或任何花哨的东西,只需将给定的十六进制值(例如FF00AB)应用于textView的可绘制形状:


列出项目。xml

<TextView
        安卓:id="@+id/icon"
        安卓:background="@drawable/rounded_corner" />

圆角。xml

<shape xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" >
    <solid 安卓:color="@color/dummyColorValue" />
</shape>

列表适配器。java

   // Set Icon Color
   String color "FF00AB";
   Drawable iconDrawable = txtIcon.getBackground();
// how to change the <solid 安卓:color>-Value of iconDrawable HERE??

假设变量颜色的十六进制值是动态的

Q:setColorFilter()是错误的方法还是我必须转换字符串


共 (1) 个答案

  1. # 1 楼答案

    我提出的解决方案很少,它肯定会帮助您找到其中一个:

    1

     GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
          bgShape.setColor(Color.BLACK); 
    

    2

    ((GradientDrawable)someView.getBackground()).setColor(someColor);
    

    3

       LayerDrawable bgDrawable = (LayerDrawable) button.getBackground();
    final GradientDrawable shape = (GradientDrawable)
            bgDrawable.findDrawableByLayerId(R.id.round_button_shape);
    shape.setColor(Color.BLACK);
    

    4

        example.setBackgroundResource(R.drawable.myshape);
    GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();
    gd.setColor(Color.parseColor("#000000"));
    gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});
    gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);
    

    希望它能帮助你:)