Wyrównaj dwa TextView, jeden z lewej w prawo, na ListView bez rozciągania tła
Więc mam dwaTextViews
na wiersz wListView
. Jeden powinien być pozostawiony, a drugi wyrównany do prawej. ObieTextViews
mają zaokrąglony prostokąt jako tło, które powinno po prostu zawinąć tekst wewnątrz. Więc wpadłem na to:
<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>
Wygląda dobrze na długie teksty, ale nie na krótsze wiadomości:
Próbowałem również LinearLayout w ten sposób:
<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>
Działa to dla krótkich wiadomości, ale nie dla dłuższych:
Czy można w jakiś sposób zmierzyć łączną szerokośćTextViews
i programowo przełączać się między tymi układami, czy robię tu coś złego?