Android-代码中修改纯色

有一个在XML中的shape,现在需要修改代码中的solid color,应该怎么实现?

<?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)rectangle.getBackground();
myGrad.setColor(Color.BLACK);

试试吧,应该可以了。