当控件背景是一个Shape时,要想改变其背景色,不能直接使用view.setBackground… 如:
<View android:id="@+id/v_test" android:layout_width="45dp" android:layout_height="45dp" android:layout_below="@id/tv_test" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:background="@drawable/v_background" />v_background.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:height="45dp" android:width="45dp"/> <solid android:color="@color/colorPrimaryDark"/> </shape>实现代码:
GradientDrawable background = (GradientDrawable) v_test.getBackground();//GradientDrawable是Drawable的子类 background.setColor(Color.RED);