我有
<?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服务调用接收到的颜色,它可以是黄色、绿色、红色或其他任何颜色。
如何基于这些信息更改形状的颜色?
View.getBackground()
返回一个GradientDrawable
而不是ShapeDrawable
。这导致在运行时尝试获取引用并以编程方式设置颜色时出现无效转换而导致应用程序崩溃。[Android形状文档] (http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)指出:*已编译资源数据类型:指向`GradientDrawable`的资源指针*。 - Luis Quijada