开发者

android:layout_gravity effect in main.xml?

开发者 https://www.devze.com 2023-01-17 21:44 出处:网络
I have 4 buttons in a horizontal linear layout. I\'d like the right edge of the 4th button to align with the rig开发者_如何学Pythonht edge of the linear layout (equal to screen width) I\'ve tried usin

I have 4 buttons in a horizontal linear layout. I'd like the right edge of the 4th button to align with the rig开发者_如何学Pythonht edge of the linear layout (equal to screen width) I've tried using android:layout_gravity="right" but it doesn't work - the right button is to the right of the 3rd one but not right aligned . Am I missing something obvious?

main.xml with only the relevant layout params only is:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
       <Button
           android:layout_height="fill_parent"
           android:layout_width="wrap_content">
       <Button SAME LAYOUT AS BUTTON 1>
       <Button SAME LAYOUT AS BUTTON 1>

       <Button
           android:layout_height="fill_parent"
           android:layout_width="wrap_content"
           android:layout_gravity="right" />
    </LinearLayout>
</LinearLayout>

This doesn't seem to match the stated behaviour for layout_gravity in the SDK reference :"Defines how to place the view, both its x- and y-axis, within its parent view group."


I know this isn't really the proper answer to your question, but you should really consider using RelativeLayout instead of LinearLayout. It scales better to different phone screen resolutions, and you can place the buttons in the correct order by using the layout_toRightOf and layout_alignParentRight attributes in your XML (or layout_above, layout_alignParentTop, etc.). Using gravity in a linear layout won't really behave as you expect because Android is still attempting to place the widgets in a fixed position.

RelativeLayout is a bit more of a pain to get working than LinearLayout, but it's worth the trouble and is also the one which Google recommends using. Once you get the hang of it, they are very convenient to work with.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号