2016-04-16 5 views
0

Ich habe eine benutzerdefinierte Ansicht, die RelativeLayout mit überschriebenen onDraw() -Methode erweitert, so dass es wie eine Nachrichtenblase aussieht. Diese Ansicht befindet sich im LinearLayout. Wenn ich versuche, diese benutzerdefinierte Ansicht mit Inhalt zu füllen, wird ein Teil ihrer Form aus dem LinearLayout entfernt. Wie Verhindern dieses Verhaltens? Wie Verhindern dieses Verhaltens?Wie kann verhindert werden, dass benutzerdefinierte Formen aus dem LinearLayout herausgedrückt werden?

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> 

Hier ist meine benutzerdefinierte Ansicht:

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

}

+0

Dies geschieht wegen Ansicht mit Gewicht = "1". Aber ohne es kann ich TextView nicht zwingen, in der Nähe der Nachricht zu sein. – user3400881

Antwort

0

haben Sie versucht das?

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

Nein) Es funktioniert nicht – user3400881

Verwandte Themen