0

I've got a weird gap in my Android layout. I've set the background colors of my views and layouts so that you can see what is where. The gray box in the middle is the gap. I've got a RelativeLayout overall (white background), with a toolbar at the top (seen in pink), then a LinearLayout which is everything else (seen in yellow). In that yellow layout I've got two children: detail information (seen in blue), and a ListView (seen in red). However, the gray area in the middle... I have no idea where that is coming from.

enter image description here

Here is my layout file (with some info clipped for readability):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
>
    <RelativeLayout
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:background="@drawable/toolbar_gradient"
        android:layout_alignParentTop="true" 
        android:background="#f0f"
       >

        <Button style="@style/Button.Done" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/toolbar"
        android:paddingTop="4dp"
        android:weightSum="10"
        android:orientation="vertical"
        android:background="#ff0"
    >
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="0dp" 
            android:layout_weight="3.5" 
            android:weightSum="9"
            android:orientation="vertical"
            android:background="#00f"
            >

        </LinearLayout>

        <ListView
            android:id="@+id/tableView"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="6.5"
            android:background="#f00" >
        </ListView>
    </LinearLayout>

</RelativeLayout>

What is happening here? What am I missing?

EDIT: It appears the layout xml may not be the problem. I've been working to reduce and reduce and reduce and find the real culprit. It's looking like my problem exists in the ListAdapter code. Will update more as I discover it.

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229
  • Probably not causing the problem, but you should not use nested weighted layouts. – Jave Apr 24 '12 at 14:32
  • I've never used weight sum before, but I read up quickly and shouldn't the weight sum be the sum of all the layouts in the container? You have your weight's set at 3.5 and 6.5 = 10, but your weight sum is 9? Does that make a difference? – Gophermofur Apr 24 '12 at 14:36
  • I can't find an issue. I copied your .xml into a layout and it fills it from top to bottom, no blank space in there. So my inclination would be that the issue is somewhere in the .xml code you left out. Do the layout_weights of those bits you left out total 9? – Barak Apr 24 '12 at 14:39
  • The weightSum is indeed the sum of all weights in that layout, however, you have a LinearLayout with weights that contains another LinearLayout with weights. This is considered to lower performance as each weighted parent has to be laid out twice. This means that if you have a weighted layout in another weighted layout they will have to be laid out 4 times, and the layout passes continue to increase exponentially. – Jave Apr 24 '12 at 14:41
  • here is a question about why nested weights are bad: http://stackoverflow.com/questions/9430764/why-are-nested-weights-bad-for-performance-alternatives – Jave Apr 24 '12 at 14:42
  • Barak, the xml I'm actually using in my project at the moment has all of the missing content commented out. I -believe- the only difference between the above and what I'm really using is that The toolbar layout uses a style definition. I removed the style definition and replaced it with all of the attributes from the style to put it all in one place. – Kenny Wyland Apr 24 '12 at 14:46
  • Just in case, I've uploaded the real files and stylesheet here, so you can test them if you'd like: http://www.inadaydevelopment.com/StackOverflow/LayoutGap/ – Kenny Wyland Apr 24 '12 at 14:58
  • It appears the layout xml may not be the problem. I've been working to reduce and reduce and reduce and find the real culprit. It's looking like my problem exists in the ListAdapter code. Will update more as I discover it. – Kenny Wyland Apr 26 '12 at 17:06
  • Oh, also, I feel you on the topic of nested weights being bad... but unless you can show me how to get a flexible UI which looks good on all sizes of devices and support Android 2.x... I'm listening. :) – Kenny Wyland Apr 26 '12 at 18:04

2 Answers2

0

Did you try removing the android:layout_alignParentBottom="true" line from the definition of your first LinearLayout?

xaviert
  • 5,653
  • 6
  • 29
  • 31
  • Yes. I actually took that line out before taking the screenshot, but forgot to update the xml I pasted above. I've removed it for clarity, but yes, it wasn't really there. – Kenny Wyland Apr 24 '12 at 14:30
0

Ok, so after what felt like endless iterations of removing things until it was fixed and adding things back in until it broke again to narrow down to the specific problem I have discovered my error. I narrowed the problem down to my styling on the layout of the cells inside the ListView above.

I created a script awhile back to help me generate many different style.xml copies for different layout sizes and such so that I can provide good font size or layout_height values for different devices and make them look right. However, due to an error in my styles template, the style.xml which was being generated with a line <item name="android:layout_height">dp</item>. The missing number before the dp caused whacky behavior.

sigh Well, THAT sucked. ;)

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229