Amit Shani
Amit Shani

Reputation: 405

Weight doesn't limit width of layout

I have a list of items I would like to show. I have written the following code
for each item's view:

<?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:paddingEnd="12dp"
    android:paddingStart="8dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp">
    <LinearLayout
        android:orientation="horizontal"    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="6"
        android:gravity="center" >
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="4dp"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="4">
            <TextView
                android:id="+@id/receipt_item_name"
                android:layout_width="wrap_content"
                android:textAlignment="viewStart"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:text="item name" />
            <TextView
                android:layout_width="wrap_content"
                android:textAlignment="viewStart"
                android:layout_height="wrap_content"
                android:layout_below="@id/receipt_item_name"
                android:text="selected by 2 others"
                android:textSize="10sp"/>
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            android:text="10$" />
    </LinearLayout>
</RelativeLayout>

But when the text in receipt_item_name is too long it makes the next
textview shift right. How can I make sure each view's weight is solid?
or What else can I do to get the same effect?

looks terrible..

Upvotes: 3

Views: 291

Answers (1)

Vucko
Vucko

Reputation: 7479

Change the width of every layout that has the weight to 0 like this:

 <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="4">

And for these two as well:

<TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAlignment="textEnd"
        android:text="10$" />

The thing is, when you set both the weight and the width, the width takes precedence and ignores the weight attribute.

Upvotes: 2

Related Questions