2016-02-08 79 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>