Getting confused using weightSum

First of all this is a layout of a fragment that contains a TextView ,View and a ListView.The View which is a gray line do not appear.

I want to get in the top the TextView and below it the gray line then the ListView.

I knew that the layout_weight < weigthSum but when using it like that I get the ListView too much in the bottom so I tried it = 165 and got a better result but still not getting the gray line

This is the xml for the layout where I have set the weightSum=100 and the layout_weight of the relative layout that contains the textView and the ImageView =165 .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">
    <!--android:weightSum="100"-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="165"
        android:id="@+id/first_relative_accueil_fragment">
        <!--165-->
        <!--android:layout_weight="160"-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/accueil_toolbar_text"
            android:text="Accueil"
            android:textSize="20dp"
            android:paddingLeft="16dp"
            android:paddingTop="16dp"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/darker_gray" />

    </RelativeLayout>
    <ListView
        android:id="@+id/accueil_fragment_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

This is what I get in my phone after running the app

1 answer

  • answered 2017-11-15 00:04 premkamal008

    <?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:orientation="vertical">
    
        <TextView
            android:id="@+id/accueil_toolbar_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="Accueil"
            android:textSize="20dp" />
    
        <View
            android:id="@+id/separator"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/darker_gray" />
    
        <ListView
            android:id="@+id/accueil_fragment_list"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
    </LinearLayout>
    

    This should serve your purpose. One needs to be a bit careful using the height definition, layout_weight, weight_sum values. (the parent-child layouts' relationships should be taken care of while defining the values).

    PS: Using of unnecessary layouts is a bad practice and makes the UI rendering slow, so try to use simpler layouts & less of nesting. :)