自定义线性布局与圆角

时间:2016-02-08 09:18:10

标签: android

我尝试绘制自定义线性布局,但我遇到的问题是我没有得到线性布局的圆角

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

我真的不知道出了什么问题。有些人请帮我解决这个问题。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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的背景。

答案 2 :(得分: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>
相关问题