agou
agou

Reputation: 738

How to make android button equal width?

I've set android:layout_weight="1" to all 3 buttons, but doesn't work.

My layout is something like:

<table1>
    <row></row>
    <row><button /><button /><button /></row>
    <row><table2><row>SOMETHING</row></table2></row>
</table1>

When table2 is hidden ("gone"), those 3 buttons seems not too bad, although they don't have equal width, when table2 is not hidden, the first button become big and I can hardly see the other 2 buttons, below are the full xml and snapshot:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Title" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText2"
                android:layout_width="fill_parent"
                android:layout_height="150dp"
                android:layout_weight="1"
                android:hint="Content"
                android:inputType="textMultiLine" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="A"
                android:text="A" />

            <Button
                android:id="@+id/button2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="B"
                android:text="B" />

            <Button
                android:id="@+id/button3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="C"
                android:text="C" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TableLayout
                android:id="@+id/optionalViewGroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_weight="1"
                > <!--  android:visibility="gone" -->

                <TableRow
                    android:id="@+id/tableRow5"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >

                    <EditText
                        android:id="@+id/editText4"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:ems="10"
                        android:hint="Tags (optional)" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow6"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >

                    <EditText
                        android:id="@+id/editText3"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:hint="Itinerary Name (optional)" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow7"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >

                    <EditText
                        android:id="@+id/editText5"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:inputType="date" />

                    <EditText
                        android:id="@+id/editText6"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:inputType="time" />
                </TableRow>
            </TableLayout>
        </TableRow>
    </TableLayout>
</ScrollView>

enter image description here enter image description here

Upvotes: 1

Views: 2047

Answers (1)

Gunnar Karlsson
Gunnar Karlsson

Reputation: 28470

When using layout_weight for horizontal View distribution, set layout_width="0dip" instead of "fill_parent" or "wrap_content" on the View with the layout_weight attribute.

See:

Android Layout Weight

android:layout_weight beginner's question.

Upvotes: 3

Related Questions