如何将两个按钮放在Android中的同一行上
只是做一个线性布局。将方向设置为水平并添加两个按钮。它准备好了,你会得到你想要的。在发布这样的问题之前,尝试使用谷歌搜索你肯定会得到答案。这段代码将帮助你。
<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>
如果你想适合布局中的两个按钮,两个按钮的权重为1。
为我工作! – Axel 2014-09-09 20:47:52
可以使用与horizonatal方向上的一个线性布局,并在其中加入
<LinearLayout
<Button1.../>
<Button2.../>
</LinearLayout>
您需要添加线性布局(水平)的两个按钮。那么你可以在一行中添加许多按钮....
您也可以使用相对布局。
这里是你的代码...
<?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>
我认为你需要使用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>
您也可以refert这个问题,以及。 http://www.mkyong.com/android/android-relativelayout-example/
希望这会帮助你。
如果您将按钮放置在LinearLayout中,请将方向值设置为“垂直”,它会自动将按钮放在同一行中。如果您使用的是RelativeLayout,那么对于一个按钮,使用android:layout_toLeftOf或android:layout_toRightOf,并将值作为其他按钮的ID。如果你的答案是正确的,请将其标记为答案。谢谢...
使用该放两个按钮放在同一行....
<?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>
最好的解决办法是把2个按钮(具有相等宽度)在LinearLayout中。
还有一件事,如果你想要相等的“宽度”按钮,然后采用0DP宽度和相同的权重按钮的所有按钮。
如果你想要相同的“高度”按钮,然后采取与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>
我真的推荐这个解决方案。在调整LinearLayout容器上的项目时,使用“layout_weight”是最好的选择。 – ivanleoncz 2016-12-08 17:48:14
感谢Ivan的评论。 – 2016-12-09 07:19:04
您需要设计一个更有意义的问题。试着给出一些更详细的信息,直到现在你已经做了什么。 – 2012-04-17 05:58:20