如何动态改变形状颜色?

cho*_*bo2 135 android shape android-layout xamarin.android

我有

<?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"
/>
Run Code Online (Sandbox Code Playgroud)

所以现在我希望这个形状根据我从Web服务调用中获取的信息来改变颜色.所以它可能是黄色或绿色或红色或其他任何取决于我从网络电话呼叫收到的颜色.

如何更改形状的颜色?根据这些信息?

Ron*_*nie 293

您可以像这样修改它

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)

  • 我得到`java.lang.ClassCastException:android.graphics.drawable.GradientDrawable在尝试这个建议时无法强制转换为android.graphics.drawable.ShapeDrawable`. (16认同)
  • 什么是btn.getBackground? (9认同)
  • 这很好用.自答案编辑以来,之前的评论已经过时了! (6认同)
  • 使用GradientDrawable bgShape =(GradientDrawable)btn.getBackground().getCurrent(); (4认同)
  • 不行.你会得到一个施法错误.需要修复或接受其他答案 (2认同)
  • 工作正常,但要注意它会改变形状颜色,以便下次使用时,它将具有您设置的最新颜色.因此,每次使用此形状可绘制时都需要设置颜色. (2认同)

Cou*_*chy 59

对我来说,它崩溃了,因为getBackground返回了一个GradientDrawable而不是一个ShapeDrawable.

所以我修改它像这样:

((GradientDrawable)someView.getBackground()).setColor(someColor);
Run Code Online (Sandbox Code Playgroud)


Man*_* V. 41

这适用于我,使用初始xml资源:

example.setBackgroundResource(R.drawable.myshape);
GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();
gd.setColor(Color.parseColor("#000000"));
gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});
gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);
Run Code Online (Sandbox Code Playgroud)

上述结果:http://i.stack.imgur.com/hKUR7.png


Fab*_*ian 10

您可以使用Java构建自己的形状.我为像Page Controler这样的iPhone做了这个,并用Java绘制形状:

/**
 * Builds the active and inactive shapes / drawables for the page control
 */
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);
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.Greez Fabian


小智 5

    LayerDrawable bgDrawable = (LayerDrawable) button.getBackground();
    final GradientDrawable shape = (GradientDrawable)
            bgDrawable.findDrawableByLayerId(R.id.round_button_shape);
    shape.setColor(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)


nil*_*lsi 5

也许其他人需要在XML中改变颜色而不需要像我需要的那样创建多个drawable.然后创建一个没有颜色的圆形绘图,然后为ImageView指定backgroundTint.

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
</shape>
Run Code Online (Sandbox Code Playgroud)

在你的布局中:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/circle"
    android:backgroundTint="@color/red"/>
Run Code Online (Sandbox Code Playgroud)

编辑:

有一个关于此方法的错误,阻止它在Android Lollipop 5.0(API级别21)上运行.但已在新版本中修复.


小智 5

circle.xml(drawable)

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid
    android:color="#000"/>

<size
    android:width="10dp"
    android:height="10dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

布局

<ImageView
      android:id="@+id/circleColor"
      android:layout_width="15dp"
       android:layout_height="15dp"
      android:textSize="12dp"
       android:layout_gravity="center"
       android:layout_marginLeft="10dp"
      android:background="@drawable/circle"/>
Run Code Online (Sandbox Code Playgroud)

在活动中

   circleColor = (ImageView) view.findViewById(R.id.circleColor);
   int color = Color.parseColor("#00FFFF");
   ((GradientDrawable)circleColor.getBackground()).setColor(color);
Run Code Online (Sandbox Code Playgroud)