需求需要动态设置控件颜色,当前控件不是直接设置背景颜色,而是通过设置shape来实现背景效果的。
Shape代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#000000"/>
<corners
android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
</shape>
获取控件的背景资源,进行背景颜色的修改:
GradientDrawable myGrad = (GradientDrawable)btn.getBackground();
myGrad.setColor(Color.RED);