2016-04-04 2 views
0

Я создал пользовательский вид, который имеет высоту и использует белую рамку. Когда я вызываю его из xml, он корректно отображается в редакторе xml, но когда я запускаю его на устройстве Marshmallow, на нем нет высоты и белого фона.Расширение CustomView RelativeLayout не показано правильно

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

чем проблема здесь? Я попробовал переопределить метод onDraw, но безрезультатно.

ответ

0

Я думаю, что ваш пользовательский RelativeLayout не может нарисовать тень вашего представления из-за своих границ с родителями.

Может быть, попытаться сделать это в родительском макете:

android:clipToPadding="false" 
Смежные вопросы