O_Casper_ne
2013-05-02 09:37
采纳率: 81.9%
浏览 12.6k

如何动态地改变 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 2013-05-06 03:10
    已采纳

    用下面的代码修改:

    ShapeDrawable bgShape = (ShapeDrawable )btn.getBackground();
    bgShape.getPaint().setColor(Color.BLACK);
    
    打赏 评论
  • 问答小助手 2013-05-06 03: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);
    }
    
    打赏 评论
  • 狼孑野心 2013-05-27 07:52

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

    打赏 评论
  • boy92517 2013-05-30 09:31

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

    p.setColor(Color.RED);

    打赏 评论
  • qq_21143291 2016-05-04 11:23
    打赏 评论

相关推荐 更多相似问题