动态改变Shape中的颜色

xiaoxiao2021-02-28  119

当控件背景是一个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);
转载请注明原文地址: https://www.6miu.com/read-58540.html

最新回复(0)