Willi Mentzel
Willi Mentzel

Reputation: 29844

Fixed second column

How can I change this layout so that the left column can "grow" to the bottom without overlapping/pushing away the second one?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="1st column"
            android:id="@+id/textView1"
            android:gravity="center_vertical"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="2nd column"
            android:id="@+id/textView2"
            android:textSize="10sp"
            android:gravity="center_vertical"/>
</LinearLayout>

Like this:
| This is a really long.........||..................|
| sentence and it doesn't.. || Some text | <-- vertically centered
| fit in the available space. ||..................|

No matter how long the sentence in the left column is, the right column should stay fixed. This is a layout for a ListView, so TableLayout is out of question.

Upvotes: 0

Views: 55

Answers (2)

Xavier Falempin
Xavier Falempin

Reputation: 1206

You can use weight in your Linear if you want to make columns

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="1st column"
        android:id="@+id/textView1"
        android:gravity="center_vertical"/>

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="2nd column"
        android:id="@+id/textView2"
        android:textSize="10sp"
        android:gravity="center_vertical"/>

the weighted size are calculated after the fixed ones took their share

Upvotes: 1

Antrromet
Antrromet

Reputation: 15414

Try using RelativeLayout.

<?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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/textView2"
        android:gravity="center_vertical"
        android:text="1st column"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:text="2nd column"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />

</RelativeLayout>

Upvotes: 1

Related Questions