如何防止自定义形状推出LinearLayout?

时间:2016-04-16 17:03:13

标签: android xml android-layout message shape

我有一个自定义视图,它使用重写的onDraw()方法扩展RelativeLayout,使其看起来像一个消息气泡。此视图位于LinearLayout中。当我尝试用内容填充此自定义视图时,其部分形状将被推出LinearLayout。如何防止这种行为?如何防止这种行为?

enter image description here

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/chat_message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_vertical"
        android:padding="10dp"
        android:text="2:46pm"
        android:textSize="@dimen/splash_4_user_name_text_size" />

    <com.mypackage.RightBasedMessage
        android:id="@+id/chat_message_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/splash_3_comment_padding"
        android:paddingLeft="@dimen/splash_3_comment_end_padding"
        android:paddingRight="@dimen/chat_message_field_beginning_padding"
        android:paddingTop="@dimen/splash_3_comment_padding">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hi :)"
            android:textColor="@color/white_text_color" />

    </com.mypackage.RightBasedMessage>

</LinearLayout>

这是我的自定义视图:

public class RightBasedMessage extends RelativeLayout {

private Paint paint;
private Path path;
private Point pointleftTop;
private Point pointRightTop;
private Point pointRightBottom;
private Point pointLeftBottom;
private float corner;
private Context context;
private int color = R.color.chat_my_message_field_color;

public RightBasedMessage(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    corner = getResources().getDimension(R.dimen.message_container_corner);
    paint = new Paint();
    path = new Path();
    paint.setStrokeWidth(5);
    paint.setStyle(Paint.Style.FILL);
    this.setWillNotDraw(false);
}

public void setMessageBackground(int color){
    this.color = color;
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int width = getWidth();
    int height = getHeight();

    pointRightTop = new Point(width-(int)corner, (int)corner/2);
    pointRightBottom = new Point(width-(int)corner, height);
    pointLeftBottom = new Point(0, height);
    pointleftTop = new Point(0, 0);
    path.reset();

    path.moveTo(corner, 0);
    path.lineTo(width, 0);
    path.quadTo(pointRightTop.x, pointRightTop.y, width-corner, corner+corner/2);
    path.lineTo(width-corner, height-corner);
    path.quadTo(pointRightBottom.x, pointRightBottom.y, width - corner*2, height);
    path.lineTo(corner, height);
    path.quadTo(pointLeftBottom.x, pointLeftBottom.y, 0, height-corner);
    path.lineTo(0, corner);
    path.quadTo(pointleftTop.x, pointleftTop.y, corner, 0);

    paint.setColor(ContextCompat.getColor(context, color));
    canvas.drawPath(path, paint);

    path.close();
}

}

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
>