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

use*_*ser 29 android android-layout android-xml android-fragments android-activity

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

这是我的图层列表:

<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>
Run Code Online (Sandbox Code Playgroud)

Suh*_*hta 55

<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>
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中添加

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

2016年10月更新

getDrawable()现在已弃用,您应该使用ContextCompat.getDrawable(context, color).

除此之外,如果你得到LayerDrawable通过findDrawableByLayerId(),那么你只好打电话view.setBackground(layerDrawable)了,这才会生效.或者,实例化也layerDrawable通过view.getBackground()工作.


Nom*_*que 19

首先,您需要为您的layer-list项目分配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>
Run Code Online (Sandbox Code Playgroud)

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

LayerDrawable shape = (LayerDrawable) getResources().getDrawable(R.drawable.your_shape)
Run Code Online (Sandbox Code Playgroud)

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

shape.setColor(Color.Black); // changing to black color
Run Code Online (Sandbox Code Playgroud)

编辑

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

LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.your_shape)
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案对我不起作用,your_shape是一个id而不是drawable.苏海尔的回答对我有用. (8认同)
  • `getDrawable()`已被弃用. (2认同)
  • LayerDrawable drawableFile = (LayerDrawable) getResources().getDrawable(R.drawable.drawable_file_name); GradientDrawable gradientDrawable = (GradientDrawable) drawableFile.findDrawableByLayerId(R.id. your_shape); (2认同)

Ket*_*ani 9

<?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>
Run Code Online (Sandbox Code Playgroud)

找到你的看法

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);
Run Code Online (Sandbox Code Playgroud)

设定颜色

outer.setColor(Color.parseColor(#000000));
inner.setColor(Color.parseColor(#FFFFFF));
Run Code Online (Sandbox Code Playgroud)