<?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服务调用的信息让shape去改变颜色。所以它可能是黄色,绿色或红色或者是从web服务调用的信息中获取的颜色。
基于这些信息,如何改变shape的颜色?
用下面的代码修改:
ShapeDrawable bgShape = (ShapeDrawable )btn.getBackground();
bgShape.getPaint().setColor(Color.BLACK);
在 java 中创建自己的 shapes
private void makeShapes() {
activeDrawable = new ShapeDrawable();
inactiveDrawable = new ShapeDrawable();
activeDrawable.setBounds(0, 0, (int) mIndicatorSize,
(int) mIndicatorSize);
inactiveDrawable.setBounds(0, 0, (int) mIndicatorSize,
(int) mIndicatorSize);
int i[] = new int[2];
i[0] = android.R.attr.textColorSecondary;
i[1] = android.R.attr.textColorSecondaryInverse;
TypedArray a = this.getTheme().obtainStyledAttributes(i);
Shape s1 = new OvalShape();
s1.resize(mIndicatorSize, mIndicatorSize);
Shape s2 = new OvalShape();
s2.resize(mIndicatorSize, mIndicatorSize);
((ShapeDrawable) activeDrawable).getPaint().setColor(
a.getColor(0, Color.DKGRAY));
((ShapeDrawable) inactiveDrawable).getPaint().setColor(
a.getColor(1, Color.LTGRAY));
((ShapeDrawable) activeDrawable).setShape(s1);
((ShapeDrawable) inactiveDrawable).setShape(s2);
}
GradientDrawable p = (GradientDrawable) viewHandle.subject_textview.getBackground();
p.setColor(Color.RED);
GradientDrawable p = (GradientDrawable) viewHandle.subject_textview.getBackground();
p.setColor(Color.RED);