2016-02-08 77 views
-1

我试着画一个自定义的线性布局,但我面临的问题是,我没有得到圆角的线性布局自定义的线性布局,圆角

public class RoundLinearLayout extends LinearLayout { 


private float radius; 
private Path path = new Path(); 
private RectF rect = new RectF(); 


public RoundLinearLayout(Context context) 
{ 
    super(context); 
    radius = 20; 
    // setWillNotDraw(false); 
} 


public RoundLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // init(context); 
} 

public RoundLinearLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // init(context); 
} 
@Override 
protected void onDraw(Canvas canvas) { 
    path.reset(); 
    rect.set(0, 0, canvas.getWidth(), canvas.getHeight()); 
    path.addRoundRect(rect, radius, radius, Path.Direction.CCW); 
    // Add 1px border RED here ? 
    path.close(); 
    canvas.clipPath(path); 
} 
} 

我真的唐诺哪里出了问题..有些人请帮我解决这个问题。

+1

不会用'corner'创建'shape'比自定义类更简单吗?只是说。 ;-) –

+0

@Geethu,请参考我的回答 – user5716019

回答

2

我会建议你用简单的CardView

使用编译依赖

compile 'com.android.support:cardview-v7:21.0.+' 

<android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
<LinearLayout 
..... 
your other child layout goes here 
</LinearLayout> 
</android.support.v7.widget.CardView> 
0

在Drawables下面使用xml。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#FFFFFF" /> 
    <stroke 
     android:width="2dp" 
     android:color="#000" /> 
    <padding 
     android:bottom="1dp" 
     android:left="1dp" 
     android:right="1dp" 
     android:top="1dp" /> 
    <corners 
     android:bottomLeftRadius="8dp" 
     android:bottomRightRadius="8dp" 
     android:radius="8dp" 
     android:topLeftRadius="8dp" 
     android:topRightRadius="8dp" /> 
</shape> 

并将其设置为LinearLayout的背景。

0

你可以选择你的颜色和设置背景的线性布局。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="4dp" /> 
    <solid android:color="#80000000" /> 
</shape>