更改特定RelativeLayout Android的颜色

时间:2014-02-27 17:15:41

标签: java android colors relativelayout draw

我有一个RelativeLayout:

   <RelativeLayout
        android:id="@+id/my_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_shape" >

my_shape.xml是这样的:

<stroke 
         android:width="1dp"
         android:color="#FFF"/>
      <solid android:color="#000"/>

所以,从我的活动中,我会改变这个RelativeLayout的颜色。 我试过

RelativeLayout tvCard = (RelativeLayout) findViewById(R.id.my_id);
tvCard.setBackgroundColor(color);

N.B。颜色是输入颜色!

通过这个解决方案,RelativeLayout完全是红色的!如何在不改变“笔触颜色”的情况下改变颜色?

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试在java代码中创建ShapeDrawable

像这样:

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(0xFFFFFFFF);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(1);
// sd1.getPaint().somehow_set_stroke_color?

ShapeDrawable sd2 = new ShapeDrawable(new RectShape());
sd2.getPaint().setColor(0xFF000000);
sd2.getPaint().setStyle(Style.STROKE);
// sd2.getPaint().somehow_set_stroke_color?
// sd2.getPaint().somehow_set_gradient_params?

layers[0] = sd1;
layers[1] = sd2;
LayerDrawable composite = new LayerDrawable(layers);
tvCard.setBackgroundDrawable(composite);