mercredi 29 juin 2016

Align two textviews so that the right one resizes until max and the left one fits in

I need a table-like view in Android which I can fill with custom name-value rows. A row consists of two horizontally-placed text views which hold the name and the value. Every row should have the following properties:

  • The left text view is aligned to parent-left, and the right one is aligned to parent-right.
  • The right text never becomes longer than 7 ems, but can be smaller.
  • The left text should fill up the remaining space in the row.
  • Obviously, the two text views should never overlap.

Currently I'm trying to solve this with a LinearLayout as the container and two TextViews wrapped in a RelativeLayout, but something's clearly out of place here (the underlined bit is clearly longer than 7 ems):

enter image description here


My XMLs:

The row layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dp"
    android:paddingTop="4dp">

    <TextView
        android:id="@+id/tb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:ellipsize="end"
        android:maxEms="7"
        android:maxLines="2" />

    <TextView
        android:id="@+id/tb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:ellipsize="end"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/tb2"
        android:layout_toStartOf="@id/tb2" />
</RelativeLayout>

The container layout into which the rows are added programatically:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:padding="16dp"/>

I'm using this layout as part of a dialog. Not sure if it adds anything to the problem or not.

Any ideas/solutions?

Aucun commentaire:

Enregistrer un commentaire