1

I may be being an idiot, but it seems like adding a Button to a horizontal LinearLayout breaks layout_gravity="top"

<?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="horizontal">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="top"
        android:text="TextView"
        android:background="#f00" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_weight="0"
        android:text="Button" />

</LinearLayout>

This looks like this (in the layout editor):

screenshot

Note that the text view is in the centre, not the top! If *all I do is remove the <button>, then it looks like this:

working screenshot

See, now it is at the top like it should be. A workaround I will use is to have layout_gravity="start" which does result in the correct behaviour:

enter image description here

But anyway, what's going on? Also see this related question. That might actually be the same problem; I'm not sure. No real answers there anyway.

Community
  • 1
  • 1
Timmmm
  • 88,195
  • 71
  • 364
  • 509

1 Answers1

4

Aha, I found the answer! You have to set android:baselineAligned="false". Never heard of that before, and kind of annoying that it is on by default!

<?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:baselineAligned="false"
    android:orientation="horizontal">

...
Timmmm
  • 88,195
  • 71
  • 364
  • 509