java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable

前端 未结 3 424
小鲜肉
小鲜肉 2021-01-01 02:50

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

3条回答
  •  时光取名叫无心
    2021-01-01 03:01

    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);
        }
     }
    

    and use this class as below

    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);
    

提交回复
热议问题