Java android在xml文件中更改颜色

Java android在xml文件中更改颜色,java,android,Java,Android,我已经在这个xml中设置了笔划颜色,但有时我想通过编程更改斯托克颜色。文件名为dummy.xml <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffffff" /> <stroke android:width

我已经在这个xml中设置了笔划颜色,但有时我想通过编程更改斯托克颜色。文件名为dummy.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff" />

    <stroke
        android:width="1dp"
        android:color="#ff000000" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>

我想以编程方式更改颜色(笔划颜色),我如何才能做到这一点

我在这里使用这个xml:

  <LinearLayout
                android:layout_marginTop="2dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/a"
                android:layout_gravity="center"
                android:background="@drawable/dummy"
                android:layout_marginBottom="2sp"
                android:orientation="horizontal"
                android:padding="5dp">

                <RelativeLayout
                    android:id="@+id/serverStatusWrapper"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dp"
                    android:background="@drawable/shadow_green"
                    android:padding="2dp">

                    <ImageView
                        android:id="@+id/serverStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:antialias="true"
                        android:src="@drawable/ic_settings_input_antenna_white_24dp" />

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/locationStatusWrapper"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/shadow_red"
                    android:padding="2dp">

                    <ImageView
                        android:id="@+id/locationStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:antialias="true"
                        android:src="@drawable/ic_warning_white_24dp" />
                </RelativeLayout>

            </LinearLayout>

您可以将笔划更改为

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setStroke(3, Color.RED);
并且可以根据需要更改固体颜色

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setColor(Color.RED);

希望这有帮助

好的,但是我在linearLayout中使用的xml,然后将imageView更改为linearLayout我尝试这样做,但我有空指针:GradientDrawable drawable=(GradientDrawable)linearLayout.getBackground();获取linearlayout的参考,让我编辑answerr@KrzysztofPokrywka我已经编辑了我的答案,希望它能起作用,不要忘记接受答案并给它打分,如果它起作用的话:)你需要动态地创建你的绘图工具,我已经尝试过了,但不适合我
GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setColor(Color.RED);