Alinee dos vistas de texto, una a la izquierda y otra a la derecha, en un Vista de lista sin estirar los fondos
Así que tengo dosTextViews
por fila en unListView
. Uno debe estar a la izquierda y el otro alineado a la derecha. AmbosTextViews
tiene un rectángulo redondeado como fondo que simplemente debe envolver el texto en el interior. Así que se me ocurrió esto:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/bubble_purple"
android:gravity="center" >
</TextView>
<TextView
android:id="@+id/text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/text_right"
android:background="@drawable/bubble_blue"
android:gravity="center" >
</TextView>
</RelativeLayout>
Se ve bien para textos largos pero no para mensajes más cortos:
También probé un LinearLayout como este:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/bubble_blue"
android:gravity="center" >
</TextView>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/bubble_purple"
android:gravity="center">
</TextView>
</LinearLayout>
Esto funciona para mensajes cortos pero no para mensajes más largos:
¿Es posible de alguna manera medir el ancho combinado de laTextViews
y cambiar programáticamente entre estos diseños, ¿o estoy haciendo algo mal aquí?