我有过
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#FFFF00" />
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
</shape>
<TextView
android:background="@drawable/test"
android:layout_height="45dp"
android:layout_width="100dp"
android:text="Moderate"
/>
所以现在我想让这个形状根据我从web服务调用中得到的信息来改变颜色。因此,它可能是黄色、绿色、红色或其他颜色,这取决于我从web服务调用中接收到的颜色。
如何更改形状的颜色?基于这些信息?
发布于 2011-08-24 01:24:13
您可以像这样简单地修改它
GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
发布于 2013-05-03 21:20:11
对我来说,它崩溃是因为getBackground
返回的是GradientDrawable
而不是ShapeDrawable
。
所以我把它修改成这样:
((GradientDrawable)someView.getBackground()).setColor(someColor);
发布于 2014-12-30 08:57:28
这对我来说很有效,使用初始的xml资源:
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);
https://stackoverflow.com/questions/7164630
复制相似问题