从自定义drawable编程运行时更改形状纯色?

时间:2017-07-31 17:03:55

标签: android colors android-drawable shapes

我想以编程方式更改我的自定义可绘制形状纯色运行时,以便如何更改该纯色,请参阅下面我创建的可绘制内容。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/gradientDrawble"
        >
        <selector>
            <item
                android:state_selected="true"
                >
                <shape
                    android:shape="oval">
                    <solid
                        android:color="@color/colorCrownCandy"/>
                </shape>
            </item>

            <item >
                <shape
                    android:shape="oval">
                    <padding
                        android:bottom="10dp"
                        android:left="10dp"
                        android:right="10dp"
                        android:top="10dp"/>
                    <solid
                        android:color="@color/colorCrownCandy"
                        />
                </shape>
            </item>
        </selector>

    </item>
    <item android:drawable="@drawable/ic_homemenu"
        />
</layer-list>

1 个答案:

答案 0 :(得分:0)

试试这个:

// The "view" is the one for which you are applying the drawable as background.

GradientDrawable backgroundForShape= (GradientDrawable)view.getBackground();
backgroundForShape.setColor(COLOR.RED); 

// You can specify the color you want. The color defines in colors.xml
backgroundForShape.setColor(ContextCompat.getColor(context, R.color.yourColor));