java.lang.ClassCastException:android.graphics.drawable.LayerDrawable不能转换为android.graphics.drawable.G

编程入门 行业动态 更新时间:2024-10-28 00:23:55
本文介绍了java.lang.ClassCastException:android.graphics.drawable.LayerDrawable不能转换为android.graphics.drawable.GradientDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用我想每个ListView项改变背景颜色。而对于我使用的形状这层列表。这里是我的code

In my application I am trying change background color for each listView item. And for that I am using shapes which in layer-list. Here is my code

drop_shadow.xml

drop_shadow.xml

<layer-list xmlns:android="schemas.android/apk/res/android"> <item> <shape android:shape="rectangle"> <gradient android:startColor="#B7B7B7" android:endColor="#A1A1A1" android:angle="270"/> <corners android:radius="10dp" /> </shape> </item> <item android:top="1px"> <shape android:shape="rectangle"> <solid android:color="@color/color11"/> <corners android:radius="10dp" /> </shape> </item> </layer-list>

main.xml中

main.xml

<RelativeLayout android:orientation="vertical" android:id="@+id/mainLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/drop_shadow"/>

当我把这种方法,我有抛出ClassCastException

When I call this method I have ClassCastException

private void setRoundedBackground(View view,int color){ GradientDrawable gradientDrawable; gradientDrawable = (GradientDrawable) view.getBackground().mutate(); gradientDrawable.setColor(getResources().getColor(color)); gradientDrawable.invalidateSelf(); }

我怎么会从LayerDrawable得到GradientDrawable?

How could I get GradientDrawable from LayerDrawable?

推荐答案

您可以动态创建渐变绘制。使用下面的类

you can create gradient drawable dynamically.. use below class

import android.graphics.drawable.GradientDrawable; public class SomeDrawable extends GradientDrawable { public SomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWidth, int pStrokeColor, float cornerRadius) { super(Orientation.BOTTOM_TOP,new int[]{pStartColor,pCenterColor,pEndColor}); setStroke(pStrokeWidth,pStrokeColor); setShape(GradientDrawable.RECTANGLE); setCornerRadius(cornerRadius); } }

和使用这个类,如下

SomeDrawable drawable = new SomeDrawable(Color.parseColor("Start Color Code"),Color.parseColor("Center Color Code"),Color.parseColor("End Color Code"),1,Color.BLACK,00); yourLayout.setBackgroundDrawable(drawable);

更多推荐

java.lang.ClassCastException:android.graphics.drawable.LayerDrawable不能转换为android

本文发布于:2023-09-21 22:23:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   ClassCastException   android   java   lang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!