如何在Android中将两个按钮放在同一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10185874/
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
How to put two buttons on same line in Android
提问by user1314147
How can I place two buttons on the same line in my login layout on my Android application?
如何在 Android 应用程序的登录布局中将两个按钮放在同一行上?
回答by Sreedev R
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。
回答by Paresh Mayani
The best solution is to place 2 buttons (Having equal width)in LinearLayout.
最好的解决方案是在 LinearLayout 中放置2 个按钮(宽度相等)。
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>
回答by Nitesh Khosla
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>
回答by Sandip Armal Patil
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>
回答by UVM
I think you need to use RelativeLayout.You can do something like this:
我认为你需要使用 RelativeLayout.You 可以这样做:
<?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.
希望这会帮助你。
回答by vipin
you can use one linear layout with horizonatal orientation and add your two buttons in it
您可以使用一种具有水平方向的线性布局并在其中添加两个按钮
<LinearLayout
<Button1.../>
<Button2.../>
</LinearLayout>
回答by Manjunath
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 内,请将 Orientation 值指定为“Vertical”,它会自动将按钮放置在同一行中。如果您使用的是 RelativeLayout,那么对于一个按钮,请使用 android:layout_toLeftOf 或 android:layout_toRightOf 并将值作为其他按钮的 ID。如果你猜对了,请把它标记为答案。谢谢...