Java Android更改XML文件中的颜色

我已经在此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>

解决方法:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.a);

您可以将笔划更改为

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setstroke(3, Color.RED);

并可以更改纯色为

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

希望这可以帮助

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...