10

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>
Luuklag
  • 3,897
  • 11
  • 38
  • 57
TrtG
  • 2,778
  • 6
  • 26
  • 39
  • 1
    set: android:layout_weight="1" for linear layout and android:layout_weight="5" for text. ie. exchange there weight. – Cropper Dec 10 '13 at 14:04
  • 2
    Yes I know this works but the question is more about why? it should have the opposite behaviour. – TrtG Dec 10 '13 at 14:07
  • 1
    This is.... wrong. Can you include a screenshot? Also, if you've changed the order of your layout(s) recently, try a clean build. – Geobits Dec 10 '13 at 14:09
  • 1
    i just copy pasted your code it works fine...linear layout shows 5 times bigger than textview. – Aravin Dec 10 '13 at 14:15
  • Thanks I found the problem thanks to your comments. See my edit. The problem came from the "wrap_content" width of my container list. If someone can explain why it produced an opposite weight behaviour... Otherwise I'll answer my post. – TrtG Dec 10 '13 at 14:23

2 Answers2

11

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".

Sufian
  • 6,405
  • 16
  • 66
  • 120
Beeing Jk
  • 3,796
  • 1
  • 18
  • 29
  • 1
    This helped me because in my example I didn't want to change the layout to match_parent. Thank you. Maybe this should be the correct answer. – Nick.D Aug 26 '15 at 22:09
4

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>
TrtG
  • 2,778
  • 6
  • 26
  • 39
  • That is an amazing find trust me! Although there is no reason why this happened, it made me find at least the point where it happened. I was inflating a fragment inside a Frame layout of the activity. My fragment was in 4:6 ratio but it behaved like 6:4 because, the Frame Layout inside activity had **android:layout_height="wrap_content"**. Changing which to match_parent did the trick! – sud007 Mar 23 '15 at 13:51