2 o casper ne O_Casper_ne 于 2013.05.02 17:37 提问

如何动态地改变 shape color?
<?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的颜色?

5个回答

hxn_217
hxn_217   2013.05.06 11:10
已采纳

用下面的代码修改:

ShapeDrawable bgShape = (ShapeDrawable )btn.getBackground();
bgShape.getPaint().setColor(Color.BLACK);
dianqiugg
dianqiugg   2013.05.27 15:52

GradientDrawable p = (GradientDrawable) viewHandle.subject_textview.getBackground();
p.setColor(Color.RED);

boy92517
boy92517   2013.05.30 17:31

GradientDrawable p = (GradientDrawable) viewHandle.subject_textview.getBackground();

p.setColor(Color.RED);

suannai0314
suannai0314 以后不要直接复制别人的答案哦
4 年多之前 回复
CSDN_Ask
CSDN_Ask   2013.05.06 11:13

在 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);
}
qq_21143291
qq_21143291   2016.05.04 19:23
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!