Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 934
ggurov Avatar asked Feb 03 '12 13:02

ggurov


3 Answers

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>
like image 75
Dmytro Danylyk Avatar answered Nov 15 '22 02:11

Dmytro Danylyk


Set LinearLayout's android:baselineAligned to false

<?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">
like image 2
xmen Avatar answered Nov 15 '22 01:11

xmen


do something like this

<?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>
like image 1
Akram Avatar answered Nov 15 '22 02:11

Akram