简体   繁体   中英

layout_weight and layout_width = 0dp in a linearlayout create an unexpected layout

i tried to put two buttons with the same size in linearlayout. my problem is that the left button isn't at the same height than the right one.

i searched for an answer but i didn't find anything related to my problem.

here a picture of what i get on an android 2.1 device: http://tinypic.com/r/eskb4n/5

i used this code: `

<LinearLayout
    android:layout_alignParentBottom="true"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:gravity="bottom">

    <Button
        android:id="@+id/take_pic_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/take_pic" />

    <Button
        android:id="@+id/from_gallery_btn"
        android:text="@string/from_gallery"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
</LinearLayout>`

thanks for your help!

EDIT: i used 0dp for layout_width instead of fill_parent..

I see 2 possible solutions:

1) set fixed android:layout_height so both buttons will definitely take equal vertical space.

<LinearLayout
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:gravity="bottom">

<Button
    android:id="@+id/take_pic_btn"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:layout_weight="1"
    android:text="@string/take_pic" />

<Button
    android:id="@+id/from_gallery_btn"
    android:text="@string/from_gallery"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:layout_weight="1"
    />

2) force text on button to take single line only.

<LinearLayout
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:gravity="bottom">

<Button
    android:id="@+id/take_pic_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true" 
    android:maxLines="1" 
    android:text="@string/take_pic" />

<Button
    android:id="@+id/from_gallery_btn"
    android:text="@string/from_gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true" 
    android:maxLines="1"
    />

You can set the android:layout_height="fill_parent" to both buttons.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM