howettl
howettl

Reputation: 12518

Inconsistent view position in ListView

I have a ListView which uses a custom adapter to display rows of data. Each row contains two TextViews; one is left-justified and one is right-justified.

Ideally what I want is for each TextView to take up at most half of the width of the row. I've accomplished this using the following row xml layout:

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

<TextView
    android:id="@+id/description"
    style="@style/ProductSpecNameFont"
    android:layout_width="fill_parent"
    android:layout_gravity="left"
    android:layout_weight="1"
    android:padding="@dimen/margin_sides"
    android:text="Display Size" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <TextView
        android:id="@+id/value"
        style="@style/ProductSpecValueFont"
        android:layout_alignParentRight="true"
        android:padding="@dimen/margin_sides"
        android:text="3.5 Inches" />
</RelativeLayout>

This "almost" works perfectly. The problem is that the rows which contain text on the right-side TextView that takes up multiple lines are not right-justified. The single-line rows are right-justified as I would like. Here is a screenshot of what I'm talking about:

Screen

Why aren't the multiple-line TextViews right-justified, and how can I make them so?

Upvotes: 0

Views: 311

Answers (2)

Joe
Joe

Reputation: 2659

Try using android:weightSum on your LinearLayout. Like this:

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

    <TextView
            android:id="@+id/description"
            style="@style/ProductSpecNameFont"
            android:layout_width="fill_parent"
            android:layout_gravity="left"
            android:layout_weight="0.5"
            android:padding="@dimen/margin_sides"
            android:text="Display Size" />

    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" >

        <TextView
                android:id="@+id/value"
                style="@style/ProductSpecValueFont"
                android:layout_alignParentRight="true"
                android:padding="@dimen/margin_sides"
                android:text="3.5 Inches" />
    </RelativeLayout>

Upvotes: 0

Aldryd
Aldryd

Reputation: 1466

Try setting the gravity of the TextView. It looks like what's happening is the TextView is aligned to the right, but the text itself is left justified.

android:gravity="right"

Upvotes: 3

Related Questions