Java CustomView扩展RelativeLayout未正确显示
我创建了一个自定义视图,该视图具有立面,并使用白色背景可绘制。当我从xml调用它时,它会正确地显示在xml编辑器上,但当我在我的棉花糖设备上运行它时,它根本就没有仰角和白色背景Java CustomView扩展RelativeLayout未正确显示,java,android,view,Java,Android,View,我创建了一个自定义视图,该视图具有立面,并使用白色背景可绘制。当我从xml调用它时,它会正确地显示在xml编辑器上,但当我在我的棉花糖设备上运行它时,它根本就没有仰角和白色背景 public class ElevatedRelativeLayout extends RelativeLayout { private Context context; public int ELEVATION = 10; public ElevatedRelativeLayout(Context context){
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));
}}
角形可绘制:
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);
}}
这里有什么问题?我尝试重写onDraw方法,但没有成功。我认为您的自定义相对论视图无法绘制视图的阴影,因为它的父边界 也许可以尝试将其放在父布局中:
android:clipToPadding="false"