如何在Android中以编程方式更改形状的纯色?
我正在尝试更改我的如何在Android中以编程方式更改形状的纯色?,android,shape,android-imagebutton,Android,Shape,Android Imagebutton,我正在尝试更改我的片段中形状的实心颜色 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/icon_circle_background"> <shape xmlns:android="http://schemas.android.com/apk/res/android" ...&g
片段中形状的实心
颜色
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon_circle_background">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
...>
<solid android:color="@color/md_cyan_400"/>
</shape>
</item>
<item
...
</item>
</layer-list>
使用以下命令:
GradientDrawable bgShape = (GradientDrawable)view.getBackground().getCurrent();
bgShape.setColor(Color.BLACK);
.getCurrent提供所选的可绘制图层列表
使用此选项将不会引发java.lang.ClassCastException:android.graphics.drawable.GradientDrawable无法强制转换为android.graphics.drawable.ShapeDrawable异常
它非常适合我。什么是非常过时的意思?它们应该工作得很好。@Bob Malooga,试过了,仍然给我以下代码:
View v = findViewById(R.id.layout_id);
LayerDrawable shape= (LayerDrawable)v.getBackground();
shape.setColor(Color.Black);
GradientDrawable bgShape = (GradientDrawable)view.getBackground().getCurrent();
bgShape.setColor(Color.BLACK);