Search code examples
androidlayoutbuttonalignmentmultiline

Buttons with multiline text sink under alignment line, how to fix it?


I see strange behavior of simple buttons, when their text splits on several lines. I have it in more complex situation, but even in the simplest one it happens. With this layout:

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

<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111" />
<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111 222 333" />
<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111 222 333 444 555 666" />
<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111" />
</LinearLayout>

the result is: buttons not in a row

Buttons go down and down with every next line of text. I want them in a line. Is this an android bug and how to fix it?


Solution

  • Try this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:layout_width="100dip"
                android:layout_height="70dip"
                android:layout_gravity="center"
                android:text="111" />
    
            <Button
                android:layout_width="100dip"
                android:layout_height="70dip"
                android:layout_gravity="center"
                android:text="111 222 333" />
    
            <Button
                android:layout_width="100dip"
                android:layout_height="70dip"
                android:layout_gravity="center"
                android:text="111 222 333 444 555 666" />
    
            <Button
                android:layout_width="100dip"
                android:layout_height="70dip"
                android:layout_gravity="center"
                android:text="111" />
        </LinearLayout>
    
    </LinearLayout>