Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_weight working opposite of what it should

In the following xml, I'm trying to draw a layout that contains two blocks (a LinearLayout and a TextView). I want the LinearLayout to be 5 times larger than the TextView. This xml produces the exact opposite of what I expected, the TextView takes 5 times more space than the LinearLayout. Notice that I have set the width of both elements to 0dp which is a common oversight.

<?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:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:layout_gravity="center_vertical"
    android:weightSum="6"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="5" >

        <TextView
            android:id="@+id/result_title_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#222222"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/result_info_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#777777"
            android:textSize="12sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/result_distance_textview"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="Arial"
        android:textColor="#ffffffff"
        android:textSize="14sp" />

</LinearLayout>

EDIT: This Layout is actually a list item which is included in this list :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/results_list_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="15dp" >

    <ListView
        android:id="@+id/search_results_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>
like image 236
TrtG Avatar asked Dec 10 '13 14:12

TrtG


People also ask

What is Layout_weight and weightSum explain with example?

android:weightSum. Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

What does Layout_weight mean?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

How views are aligned inside a LinearLayout?

By default, everything in a linear layout is Start and Top aligned. If you create a row of widgets by using a horizontal LinearLayout, the row will be push to the start/left side of the screen. If you create a set of widgets using a vertical LinearLayout, the column will be push to the top side of the screen.

How to divide Linear layout in android studio?

If you want to divide layout into two parts of same size you have to set layout_width attributes to 0dp of both layouts and layout_weight to 1 inside the main layout. Then set weightSum to 2 of the parent layout. Same concept can be applied to divide layout to any ratio.


2 Answers

I also just met the same problem, and the way of solving is to change:

  • width of child view to 0dp if parent LinearLayout has orientation="horizontal";

  • height of child view to 0dp if parent LinearLayout has orientation="vertical".

like image 132
Beeing Jk Avatar answered Nov 02 '22 21:11

Beeing Jk


The issue came from the container list which had a "wrap_content" width. Changing to "match_parent" fixed the problem.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/results_list_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="15dp" >

    <ListView
        android:id="@+id/search_results_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>
like image 41
TrtG Avatar answered Nov 02 '22 22:11

TrtG