Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 以编程方式更改图层列表中的形状颜色大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何以编程方式更改图层列表中形状的颜色(#000000)?

这是我的图层列表:

<@L_674_0@ xmlns:android="http://scheR_95_11845@as.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>
</@L_674_0@>

解决方法

首先,您需要为您分配ID到层列表项.
<@L_674_0@ xmlns:android="http://scheR_95_11845@as.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>
</@L_674_0@>

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

LayerDrawable shape = (LayerDrawablE) getresources().getDrawable(R.drawable.your_shapE)

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

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

编辑

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

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

大佬总结

以上是大佬教程为你收集整理的android – 以编程方式更改图层列表中的形状颜色全部内容,希望文章能够帮你解决android – 以编程方式更改图层列表中的形状颜色所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。