以编程方式更改图层列表中的形状颜色

时间:2021-06-15 21:12:11

How can I programmatically change the color (#000000) of a shape in a layer list?

如何以编程方式更改图层列表中形状的颜色(#000000)?

Here is my layer list:

这是我的图层列表:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> // CHANGE THIS COLOR
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

4 个解决方案

#1


14  

First of all you need to assign id to you layer-list item.

首先,您需要为您的图层列表项指定ID。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First assign id to the list item-->
    <item  android:id="@+id/your_shape">  
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> 
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

Then get your shape by id.

然后通过id获取你的形状。

LayerDrawable shape = (LayerDrawable) getResources().getDrawable(R.drawable.your_shape)

And you can change the color of your shape by calling

你可以通过调用改变你的形状的颜色

shape.setColor(Color.Black); // changing to black color

EDIT

As getDrawable() has been deprecated. Use the following line of code.

由于getDrawable()已被弃用。使用以下代码行。

LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.your_shape)

#2


40  

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/gradientDrawble"> // Give id
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> // CHANGE THIS COLOR
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

Then in you code just add

然后在你的代码中添加

LayerDrawable layerDrawable = (LayerDrawable) getResources()
            .getDrawable(R.drawable.my_drawable);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
            .findDrawableByLayerId(R.id.gradientDrawble);
gradientDrawable.setColor(color); // change color

Update Oct-2016

getDrawable() is now deprecated, you should use ContextCompat.getDrawable(context, color) instead.

现在不推荐使用getDrawable(),您应该使用ContextCompat.getDrawable(context,color)。

Beside, if you get the LayerDrawable by findDrawableByLayerId(), then you had to call view.setBackground(layerDrawable) for this to take effect. Alternatively, instantiating the layerDrawable by view.getBackground() also worked.

此外,如果通过findDrawableByLayerId()获取LayerDrawable,则必须调用view.setBackground(layerDrawable)才能使其生效。或者,通过view.getBackground()实例化layerDrawable也有效。

#3


0  

You can also get the drawable or background of the view and set it as follows.

您还可以获取视图的可绘制或背景,并将其设置如下。

LayerDrawable layerDrawable = (LayerDrawable) view.getDrawable(); //view.getBackground()
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
                .findDrawableByLayerId(R.id.layer_list_item_id);
gradientDrawable.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));

#4


0  

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

    <item android:id="@+id/outer">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <padding android:top="3dp" android:bottom="3dp" android:right="3dp" android:left="3dp"/>
            <stroke
                android:width="1dp"
                android:color="#ffffff"/>
        </shape>
    </item>

    <item android:id="@+id/middle">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <padding android:top="2dp" android:bottom="2dp" android:right="2dp" android:left="2dp"/>
            <stroke
                android:width="3dp"
                android:color="#ffffff"/>
        </shape>
    </item>

    <item android:id="@+id/inner">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <solid
                android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

Find your view

找到你的观点

LayerDrawable layerDrawable = (LayerDrawable) yourView.getBackground();
GradientDrawable outer = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.outer);
GradientDrawable middle = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.middle);
GradientDrawable inner = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.inner);

Set color

outer.setColor(Color.parseColor(#000000));
inner.setColor(Color.parseColor(#FFFFFF));

#1


14  

First of all you need to assign id to you layer-list item.

首先,您需要为您的图层列表项指定ID。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First assign id to the list item-->
    <item  android:id="@+id/your_shape">  
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> 
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

Then get your shape by id.

然后通过id获取你的形状。

LayerDrawable shape = (LayerDrawable) getResources().getDrawable(R.drawable.your_shape)

And you can change the color of your shape by calling

你可以通过调用改变你的形状的颜色

shape.setColor(Color.Black); // changing to black color

EDIT

As getDrawable() has been deprecated. Use the following line of code.

由于getDrawable()已被弃用。使用以下代码行。

LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.your_shape)

#2


40  

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/gradientDrawble"> // Give id
        <shape android:shape="rectangle">
            <solid android:color="#000000" /> // CHANGE THIS COLOR
        </shape>
    </item>
    <item android:left="5dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/bg" />
        </shape>
    </item>
</layer-list>

Then in you code just add

然后在你的代码中添加

LayerDrawable layerDrawable = (LayerDrawable) getResources()
            .getDrawable(R.drawable.my_drawable);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
            .findDrawableByLayerId(R.id.gradientDrawble);
gradientDrawable.setColor(color); // change color

Update Oct-2016

getDrawable() is now deprecated, you should use ContextCompat.getDrawable(context, color) instead.

现在不推荐使用getDrawable(),您应该使用ContextCompat.getDrawable(context,color)。

Beside, if you get the LayerDrawable by findDrawableByLayerId(), then you had to call view.setBackground(layerDrawable) for this to take effect. Alternatively, instantiating the layerDrawable by view.getBackground() also worked.

此外,如果通过findDrawableByLayerId()获取LayerDrawable,则必须调用view.setBackground(layerDrawable)才能使其生效。或者,通过view.getBackground()实例化layerDrawable也有效。

#3


0  

You can also get the drawable or background of the view and set it as follows.

您还可以获取视图的可绘制或背景,并将其设置如下。

LayerDrawable layerDrawable = (LayerDrawable) view.getDrawable(); //view.getBackground()
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
                .findDrawableByLayerId(R.id.layer_list_item_id);
gradientDrawable.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));

#4


0  

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

    <item android:id="@+id/outer">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <padding android:top="3dp" android:bottom="3dp" android:right="3dp" android:left="3dp"/>
            <stroke
                android:width="1dp"
                android:color="#ffffff"/>
        </shape>
    </item>

    <item android:id="@+id/middle">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <padding android:top="2dp" android:bottom="2dp" android:right="2dp" android:left="2dp"/>
            <stroke
                android:width="3dp"
                android:color="#ffffff"/>
        </shape>
    </item>

    <item android:id="@+id/inner">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <size android:height="40dp" android:width="40dp"/>
            <solid
                android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

Find your view

找到你的观点

LayerDrawable layerDrawable = (LayerDrawable) yourView.getBackground();
GradientDrawable outer = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.outer);
GradientDrawable middle = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.middle);
GradientDrawable inner = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.inner);

Set color

outer.setColor(Color.parseColor(#000000));
inner.setColor(Color.parseColor(#FFFFFF));