I created a custom view which has an elevation and uses a white background drawable. When I call it from xml , it is shown correctly on the xml editor but when I run it on my Marshmallow device , there is no elevation and white background at all.
public class ElevatedRelativeLayout extends RelativeLayout {
private Context context;
public int ELEVATION = 10;
public ElevatedRelativeLayout(Context context){
super(context);
this.context = context;
setAttributes();
}
public ElevatedRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setAttributes();
}
public ElevatedRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setAttributes();
}
private void setAttributes (){
if(Build.VERSION.SDK_INT>=21)
this.setElevation(ELEVATION);
this.setBackgroundDrawable(new CornerShapedDrawable(context, Color.WHITE , GradientDrawable.RECTANGLE, 30));
}}
CornerShapedDrawable :
public class CornerShapedDrawable extends Drawable {
int type = 0 ;
float corners = 0 ;
int backgroundColor;
public CornerShapedDrawable (Context context , int backgroundColor ,int type , float corners){
this.type = type;
this.corners = corners;
this.backgroundColor = backgroundColor;
}
@Override
public void draw(Canvas canvas) {
float[] f = new float[]{corners,corners,corners,corners,0,0,0,0};
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadii(f);
shape.setShape(type);
shape.setColor(backgroundColor);
shape.setStroke(3 , Color.WHITE);
}}
What's the problem in here? I tried overriding the onDraw method but to no avail.
答案 0 :(得分:0)
I think your custom RelativeLayout can't draw the shadow of your view because of its parent bounds.
Maybe try to put this in the parent layout :
android:clipToPadding="false"