locked
layout format listview RRS feed

  • Question

  • User319921 posted

    I have a listview with the following layout:

    <?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="fill_parent"
        android:paddingLeft="5dp"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:weightSum="100">
        <TextView
            android:text="City"
            android:layout_width="match_parent"
            android:layout_weight="25"
            android:layout_height="wrap_content"
            android:id="@+id/textCity" />
        <TextView
            android:text="Name"
            android:layout_width="match_parent"
            android:layout_weight="25"
            android:layout_height="wrap_content"
            android:id="@+id/textName" />
        <TextView
            android:text="Wins"
            android:layout_width="match_parent"
            android:layout_weight="25"
            android:layout_height="wrap_content"
            android:id="@+id/textWins" />
        <TextView
            android:text="Loses"
            android:layout_width="match_parent"
            android:layout_weight="25"
            android:layout_height="wrap_content"
            android:id="@+id/textLoses" />
    </LinearLayout>
    

    The layout_weight is 25 for each cell.

            android:layout_weight="25"
    

    I want to space out the two left most columns for more width, so I am changing their weight to 30 and the other two weights (two right most) to 20.

    <?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="fill_parent"
        android:paddingLeft="5dp"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:weightSum="100">
        <TextView
            android:text="City"
            android:layout_width="match_parent"
            android:layout_weight="30"
            android:layout_height="wrap_content"
            android:id="@+id/textCity" />
        <TextView
            android:text="Name"
            android:layout_width="match_parent"
            android:layout_weight="30"
            android:layout_height="wrap_content"
            android:id="@+id/textName" />
        <TextView
            android:text="Wins"
            android:layout_width="match_parent"
            android:layout_weight="20"
            android:layout_height="wrap_content"
            android:id="@+id/textWins" />
        <TextView
            android:text="Loses"
            android:layout_width="match_parent"
            android:layout_weight="20"
            android:layout_height="wrap_content"
            android:id="@+id/textLoses" />
    </LinearLayout>
    

    But the display is not what I want.

    What am I doing wrong? It must be something basic.

    Thursday, June 8, 2017 11:17 PM

Answers

  • User27078 posted

    Hi, you have to set android:layout_width="0" for each TextView. This means all TextViews have Width zero at the beginning and then the total width will be distributed to them according to their weight.

    <?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="fill_parent"
        android:paddingLeft="5dp"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:weightSum="100">
        <TextView
            android:text="City"
            android:layout_width="0"
            android:layout_weight="30"
            android:layout_height="wrap_content"
            android:id="@+id/textCity" />
        <TextView
            android:text="Name"
            android:layout_width="0"
            android:layout_weight="30"
            android:layout_height="wrap_content"
            android:id="@+id/textName" />
        <TextView
            android:text="Wins"
            android:layout_width="0"
            android:layout_weight="20"
            android:layout_height="wrap_content"
            android:id="@+id/textWins" />
        <TextView
            android:text="Loses"
            android:layout_width="0"
            android:layout_weight="20"
            android:layout_height="wrap_content"
            android:id="@+id/textLoses" />
    </LinearLayout>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 9, 2017 6:50 AM

All replies

  • User27078 posted

    Hi, you have to set android:layout_width="0" for each TextView. This means all TextViews have Width zero at the beginning and then the total width will be distributed to them according to their weight.

    <?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="fill_parent"
        android:paddingLeft="5dp"
        android:paddingBottom="2dp"
        android:paddingTop="2dp"
        android:weightSum="100">
        <TextView
            android:text="City"
            android:layout_width="0"
            android:layout_weight="30"
            android:layout_height="wrap_content"
            android:id="@+id/textCity" />
        <TextView
            android:text="Name"
            android:layout_width="0"
            android:layout_weight="30"
            android:layout_height="wrap_content"
            android:id="@+id/textName" />
        <TextView
            android:text="Wins"
            android:layout_width="0"
            android:layout_weight="20"
            android:layout_height="wrap_content"
            android:id="@+id/textWins" />
        <TextView
            android:text="Loses"
            android:layout_width="0"
            android:layout_weight="20"
            android:layout_height="wrap_content"
            android:id="@+id/textLoses" />
    </LinearLayout>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 9, 2017 6:50 AM
  • User319921 posted

    That's it! Thanks for the help.

    Friday, June 9, 2017 3:31 PM