在用作背景的Drawable xml中在运行时更改形状纯色

ʞᴉɯ*_*ʞᴉɯ 29 layout android shape drawable

我有一个Drawable xml文件(background.xml):

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
         ...........
        </shape>
    </item>

    <item android:id="@+id/shape_id">
        <shape android:shape="rectangle">
            <solid android:color="#ffefefef" /> 
        </shape>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

由LinearLayout使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background"
    android:id="@+id/layout_id"
    >
Run Code Online (Sandbox Code Playgroud)

现在我需要shape_id根据某些条件在运行时更改形状纯色.这该怎么做?

ʞᴉɯ*_*ʞᴉɯ 61

发现我:

    View v = findViewById(R.id.layout_id);

    LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
    final GradientDrawable shape = (GradientDrawable)   bgDrawable.findDrawableByLayerId(R.id.shape_id);
    shape.setColor(----);
Run Code Online (Sandbox Code Playgroud)

  • 您是否知道如何在RemoteView上为小部件实现相同的功能?谢谢. (3认同)