Buttons with multiline text flow in the alignment line, how to fix it?

I see the strange behavior of simple buttons when their text is split into several lines. I have this in a more difficult situation, but even in the simplest case this happens. With this arrangement:

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

result: buttons not in a row

Buttons go down and down with each next line of text. I want them in line. This is an android error and how to fix it?

+5
source share
5 answers

Try the following:

<?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>
+9
source

Set LinearLayout android:baselineAlignedtofalse

<?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">
+2
source

-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btn1"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111" />

<Button
    android:layout_toRightOf="@+id/btn1"
    android:id="@+id/btn2"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111 222 333" />

<Button
    android:layout_toRightOf="@+id/btn2"
    android:id="@+id/btn3"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111 222 333 444 555 666" />

<Button
           android:layout_toRightOf="@+id/btn3"
    android:id="@+id/btn4"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111" />

</RelativeLayout>
+1

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="111" />
0

, .

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

If you want these buttons to stretch the entire width, add the line below, as well as each button

    android:layout_weight="1" 
0
source

All Articles