<?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服务调用回信息,这种形状改变颜色。所以它可能是黄色或绿色或红色或任何取决于我从网络服务调用收到的颜色。
如何更改形状的颜色?基于这些信息?
由@Couitchy方法指定'View.getBackground()'返回'GradientDrawable'而不是'ShapeDrawable'导致应用程序在运行时崩溃,原因是尝试获取引用时无效转换并以编程方式设置颜色。 [Android Shape doc](http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)指出:*编译的资源数据类型: 指向'GradientDrawable' *的资源指针。 –