动态改变Shape中的颜色

来源:互联网 发布:linxu修改ssh端口号 编辑:程序博客网 时间:2024/05/20 21:58

当控件背景是一个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);
原创粉丝点击