BoomingBones
BoomingBones

Reputation: 26

Left view will take up all the space in a horizontal LinearLayout

I have two TextView (tv1 and tv2), and I want tv2 to be on the right of tv1, like this:

short text

or

long text

(Sorry I can't embed images because I don't have enough reputation : (

So I make a layout like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="*" />

</LinearLayout>

If the text of tv1 is within one line, everything is perfect. But if the text is more than one line, tv1 will take all the space:

tv1 take all the space

I think it may be because of Android lays out views in the order in which they appear in the layout file, so I change the root view from LinearLayour to ConstraintLayout and make tv2 first:

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="*"
    app:layout_constraintLeft_toRightOf="@+id/tv1"
    app:layout_constraintStart_toEndOf="@+id/tv1"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This still doesn't work. I know I can use setLayoutParams to set tv1's width programmatically, but is there any other way besides programming?

Upvotes: 0

Views: 221

Answers (2)

BoomingBones
BoomingBones

Reputation: 26

Finally found a perfect way to archive this:

  1. Change LinearLayout to TableLayout + TableRow

  2. Use the shrinkColumns attribute of TableLayout to specify the zero-based index of the columns to shrink. I set it to 0 because I want tv1 to be shrinkable so that it will not take up all the space

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="*" />
    
        </TableRow>
    </TableLayout>
    

Upvotes: 0

hata
hata

Reputation: 12478

At last I managed to make it by using padding:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="12sp"
        android:text="HelloWorld"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="12sp"
        android:layout_height="wrap_content"
        android:text="*"
        app:layout_constraintRight_toRightOf="@id/tv1"
        app:layout_constraintTop_toTopOf="@id/tv1" />

</androidx.constraintlayout.widget.ConstraintLayout>

The point is using tv2's right-to-right constraint for tv1, and setting a fixed size width of tv2 and padding-right size of tv1. So tv2 overlays on top (and inside) of tv1.

screenshot

Upvotes: 1

Related Questions