在Android中如何将两个按钮放在同一条线上

时间:2022-09-29 14:03:04

How can I place two buttons on the same line in my login layout on my Android application?

如何在Android应用程序的登录布局中在同一行上放置两个按钮?

7 个解决方案

#1


30  

Just make a linear layout.Set the orientation to horizontal and add two buttons.Its ready you will get what you want.Before posting such questions try googling you will get the answer for sure.This piece of code will help you.

做一个线性布局。将方向设置为水平,并添加两个按钮。准备好了,你会得到你想要的。在发布这些问题之前,试着在谷歌上搜索一下,你肯定会得到答案。这段代码将对您有所帮助。

<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="Button" />

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

</LinearLayout>

if you want to fit the two buttons in the layout.give the weight as 1 for both the buttons.

如果你想在布局中安装两个按钮。两个按钮的权重都为1。

#2


7  

The best solution is to place 2 buttons (Having equal width) in LinearLayout.

最好的解决方案是在线性布局中放置两个按钮(具有相同的宽度)。

One more thing, If you want equal "width" buttons then take button with 0dp width and same weights to all the buttons.

还有一件事,如果你想要相同的“宽度”按钮,那就选择0dp宽度和相同重量的按钮。

And if you want equal "hight" buttons then take button with 0dp height and same weights to all the buttons.

如果你想要同样的“高度”按钮,那就选择高度为0dp、重量为相同的按钮。

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

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

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

</LinearLayout>

#3


4  

Use this put two button on the same line....

使用这把两个按钮在同一行....

<?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"

   >
    <Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:layout_alignParentBottom="true"
    />
      <Button
    android:id="@+id/cancel"     
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel"
    android:layout_toRightOf="@+id/login"
    android:layout_alignParentBottom="true"
    />
      </RelativeLayout>

#4


2  

you can use one linear layout with horizonatal orientation and add your two buttons in it

您可以使用一个线性布局与水平方向,并添加您的两个按钮。

<LinearLayout

   <Button1.../>
   <Button2.../>
</LinearLayout>

#5


2  

You need to add Linear Layout(Horizontal). then you can add many button in one line....
You can also use Relative layout for this.
Here is code for you...

您需要添加线性布局(水平)。然后您可以添加许多按钮在一行....也可以使用相对布局。这是你的密码……

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

#6


1  

I think you need to use RelativeLayout.You can do something like this:

我想你需要使用相对布局。你可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:layout_width="fill_parent">

    <Button

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

    <Button

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

</RelativeLayout>

Also you can refert this as well. http://www.mkyong.com/android/android-relativelayout-example/

你也可以参考这个。http://www.mkyong.com/android/android-relativelayout-example/

Hope this will help you.

希望这能对你有所帮助。

#7


1  

If you are placing the buttons inside the LinearLayout, give the Orientation value as "Vertical", it will automatically place the buttons in the same line. If you are using RelativeLayout, then for one button use android:layout_toLeftOf OR android:layout_toRightOf and give value as ID of other button. If you have got it right, please mark it as the answer. Thanks...

如果您将按钮放置在LinearLayout中,将方向值设为“Vertical”,它将自动将按钮放置在同一行。如果你正在使用相对布局,那么对于一个按钮,使用android:layout_toLeftOf或android:layout_toRightOf,并将值作为其他按钮的ID。如果你答对了,请把它标为答案。谢谢……

#1


30  

Just make a linear layout.Set the orientation to horizontal and add two buttons.Its ready you will get what you want.Before posting such questions try googling you will get the answer for sure.This piece of code will help you.

做一个线性布局。将方向设置为水平,并添加两个按钮。准备好了,你会得到你想要的。在发布这些问题之前,试着在谷歌上搜索一下,你肯定会得到答案。这段代码将对您有所帮助。

<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="Button" />

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

</LinearLayout>

if you want to fit the two buttons in the layout.give the weight as 1 for both the buttons.

如果你想在布局中安装两个按钮。两个按钮的权重都为1。

#2


7  

The best solution is to place 2 buttons (Having equal width) in LinearLayout.

最好的解决方案是在线性布局中放置两个按钮(具有相同的宽度)。

One more thing, If you want equal "width" buttons then take button with 0dp width and same weights to all the buttons.

还有一件事,如果你想要相同的“宽度”按钮,那就选择0dp宽度和相同重量的按钮。

And if you want equal "hight" buttons then take button with 0dp height and same weights to all the buttons.

如果你想要同样的“高度”按钮,那就选择高度为0dp、重量为相同的按钮。

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

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

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

</LinearLayout>

#3


4  

Use this put two button on the same line....

使用这把两个按钮在同一行....

<?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"

   >
    <Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:layout_alignParentBottom="true"
    />
      <Button
    android:id="@+id/cancel"     
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel"
    android:layout_toRightOf="@+id/login"
    android:layout_alignParentBottom="true"
    />
      </RelativeLayout>

#4


2  

you can use one linear layout with horizonatal orientation and add your two buttons in it

您可以使用一个线性布局与水平方向,并添加您的两个按钮。

<LinearLayout

   <Button1.../>
   <Button2.../>
</LinearLayout>

#5


2  

You need to add Linear Layout(Horizontal). then you can add many button in one line....
You can also use Relative layout for this.
Here is code for you...

您需要添加线性布局(水平)。然后您可以添加许多按钮在一行....也可以使用相对布局。这是你的密码……

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

#6


1  

I think you need to use RelativeLayout.You can do something like this:

我想你需要使用相对布局。你可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:layout_width="fill_parent">

    <Button

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

    <Button

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

</RelativeLayout>

Also you can refert this as well. http://www.mkyong.com/android/android-relativelayout-example/

你也可以参考这个。http://www.mkyong.com/android/android-relativelayout-example/

Hope this will help you.

希望这能对你有所帮助。

#7


1  

If you are placing the buttons inside the LinearLayout, give the Orientation value as "Vertical", it will automatically place the buttons in the same line. If you are using RelativeLayout, then for one button use android:layout_toLeftOf OR android:layout_toRightOf and give value as ID of other button. If you have got it right, please mark it as the answer. Thanks...

如果您将按钮放置在LinearLayout中,将方向值设为“Vertical”,它将自动将按钮放置在同一行。如果你正在使用相对布局,那么对于一个按钮,使用android:layout_toLeftOf或android:layout_toRightOf,并将值作为其他按钮的ID。如果你答对了,请把它标为答案。谢谢……