简体   繁体   中英

How to position buttons in linear layout

I have two buttons in a horizontal LinearLayout. They are currently next to each other and the very left. I want to move the second button to the right end of the LinearLayout. I tried android:gravity on these buttons but this didn't change the position of them at all. Thanks

The easiest way to do this is to use RelativeLayout . You can give your Button you want on the right the property alignParentRight="true" .

<Button
    ...
    android:layout_alignParentRight="true"/>

For a horizontal LinearLayout , android:layout_graivty (which is what you would want instead of android:gravity ) left and right won't do anything because the Views are already placed from right to left.

See this answer on the difference between android:gravity and android:layout_gravity if you are uncertain about those.

Edit

Depending on exactly what you need/want, it is possible to do this with a LinearLayout though probably still much easier and more flexible with a RelativeLayout . Anyway, you can use weight to achieve something similar and play with the values. The following gives me a Button on the left and a Button on the right.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Left Button"/>
<View
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="Right Button"/>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Right Button"/>
</LinearLayout>

You cannot achieve this using a LinearLayout .
Use a RelativeLayout instead and place each button relative to RelativeLayout right or left. Something like below example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:text="Button" />

</RelativeLayout>

Try setting the left button's layout_gravity to left (or start ) and the right button's layout_gravity to right (or end ).

The problem is that you are currently using gravity which

Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

Instead, you should use layout_gravity that is

Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.

In other words - you are currently telling the buttons how to align their child views, instead of telling them how to be aligned within their parent.

您可以设置android:layout_weight='1'并且两个按钮将平均共享屏幕(并排),或者如果您想要它们之间的额外空间,您可以在线性布局中放置一个按钮并设置android:layout_gravityleftright的每个。

Add a RelativeLayout and set values to layout_marginLeft , layout_marginTop , etc.

eg. android:layout_marginLeft="32dp" android:layout_marginTop="160dp"

使 linearLayout 方向垂直,并根据需要设置按钮的重力 =>中心

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