Alinhar duas TextViews, uma à esquerda da outra, em um ListView sem esticar os fundos
Então eu tenho doisTextViews
por linha em umListView
. Um deve ser deixado e o outro alinhado à direita. AmbosTextViews
tem um retângulo arredondado como plano de fundo, que deve apenas envolver o texto dentro. Então eu vim com isso:
<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>
Parece bom para textos longos, mas não para mensagens curtas:
Eu também tentei um LinearLayout assim:
<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>
Isso funciona para mensagens curtas, mas não para mensagens mais longas:
É de alguma forma possível medir a largura combinada doTextViews
e programaticamente alternar entre esses layouts, ou estou fazendo algo errado aqui?