Android中左右对齐两键布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11222916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:23:10  来源:igfitidea点击:

Align two-button layout left and right in Android

android

提问by Dinesh

I want to align two buttons with a linear layout, one on the left, and one on the right, like the next and previous buttons on an image gallery. I tried to align them but it doesn't work.

我想将两个按钮与线性布局对齐,一个在左侧,一个在右侧,就像图片库中的下一个和上一个按钮一样。我试图对齐它们,但它不起作用。

XML layout code:

XML布局代码:

<?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"
    android:background="@android:color/white"
    android:gravity="bottom" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

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

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next" />

    </LinearLayout>

</LinearLayout>

Actual output:

实际输出:

Expected output:

预期输出:

How can I fix it?

我该如何解决?

回答by D-32

Use a RelativeLayout. There you can set android:layout_alignParentLeftand android:layout_alignParentRight. This should work for you:

使用一个RelativeLayout. 在那里你可以设置android:layout_alignParentLeftandroid:layout_alignParentRight。这应该适合你:

<?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"
    android:background="@android:color/white"
    android:gravity="bottom" >


    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

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

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

    </RelativeLayout>

</LinearLayout>

回答by App Kart

With Linear Layout

使用线性布局

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Prev"/>
        </LinearLayout>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"/>

</LinearLayout>

With Relative Layout

使用相对布局

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Prev"
            android:layout_alignParentLeft="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

回答by Chintan Raghwani

Use Relative layout in your LinearLayout;

在您的LinearLayout; 中使用相对布局

Also add android:layout_alignParentLeft="true"to "prev" Button

也添加 android:layout_alignParentLeft="true"到“prev”Button

and android:layout_alignParentRight="true"to "next" Button

android:layout_alignParentRight="true"“下一步”Button

<?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:background="@android:color/white"
    android:gravity="bottom"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"    <---- ADD this prop
            android:text="prev" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"    <----- ADD this prop
            android:text="next" />
    </RelativeLayout>

</LinearLayout>

回答by Khan

ur layout xml should be as shown below

你的布局xml应该如下所示

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom"
    android:background="#fff">


 <RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000"
    android:layout_gravity="bottom" >
    <Button      
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:text="Pre" />

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

回答by ariefbayu

Something like this:

像这样的东西:

<?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:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/button1"
            android:orientation="vertical" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

The idea is, to create a RelativeLayout as container and put first Button and then stick it to the right of parent view. After that, add Another button insideLinearLayout and set this LinearLayout into the left of first Button.

这个想法是,创建一个RelativeLayout作为容器并放置第一个Button,然后将其粘贴到父视图的右侧。之后,LinearLayout 中添加另一个按钮,并将这个 LinearLayout 设置到第一个 Button 的左侧。

Here's the result.

结果如下。

enter image description here

在此处输入图片说明

回答by ScouseChris

Use a RelativeLayoutinstead of a LinearLayoutyou can add the tags android:layout_alignParentRight="true"and android:layout_alignParentLeft="true"on each button.

使用 aRelativeLayout代替 aLinearLayout您可以在每个按钮上添加标签android:layout_alignParentRight="true"android:layout_alignParentLeft="true"

回答by SERPRO

You can use RelativeLayout like this:

你可以像这样使用RelativeLayout:

<?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:background="@android:color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:gravity="bottom"
        android:orientation="horizontal" >

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

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

</LinearLayout>

(I used grey instead of white for the screenshot) LAYOUT

(我在截图中使用灰色而不是白色) 布局

回答by Marco Luglio

Use a FrameLayout and add the layout_gravity attribute in each of the buttons

使用 FrameLayout 并在每个按钮中添加 layout_gravity 属性

回答by Shahryar

Use margin_Start and End to seprate the buttons in the layout

使用 margin_Start 和 End 分隔布局中的按钮

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
    >

    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="-:Linear Layout:-"
              android:textSize="35sp"
              android:layout_gravity="center"
              android:layout_marginTop="250dp"
              tools:ignore="HardcodedText"/>

        <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="90dp">

        <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Relative"
                tools:ignore="ButtonStyle,HardcodedText"/>

        <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="200dp"
                android:text="Constraint"
                tools:ignore="ButtonStyle"
        />
        </LinearLayout>
    </LinearLayout>
    [enter image description here][1]


      [1]: https://i.stack.imgur.com/NMYj2.png

回答by Simon Peres

I guess, the simplest solution is to use View in your inner LinearLayout.

我想,最简单的解决方案是在您的内部 LinearLayout 中使用 View 。

<LinearLayout
    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:text="prev"/>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="next"/>

</LinearLayout>