I have a RelativeLayout
with two buttons at bottom which are side by side. My goal is to have those buttons side by side but filling the screen width. Can somebody tell me how to do it?
我有一个RelativeLayout,底部有两个按钮并排。我的目标是将这些按钮并排放置,但填充屏幕宽度。有人能告诉我怎么做吗?
My layout file is:
我的布局文件是:
<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="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_weight="1"
android:layout_marginTop="77dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
5 个解决方案
#1
7
Put your Buttons in a LinearLayout
which has horizontal orientation. And assign weight to your buttons.
将您的按钮放在具有水平方向的LinearLayout中。并为您的按钮分配重量。
Here's your code but modified.
这是您的代码,但已修改。
<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">\
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_weight="0.5"
android:text="Left Button"
/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Right Button"
/>
</LinearLayout>
#2
7
<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/dummyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/dummyView"
android:text="Left Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/dummyView"
android:text="Right Button" />
#3
0
It's very simple, if you see in the palette for the buttons you can see the attribute: "layout:alignComponent" very useful to align all views in a RelativeLayout; So you can align a left side of button2 to right side of button1... Simple... No LinearLayout needed...
这很简单,如果你在调色板中看到按钮,你可以看到属性:“layout:alignComponent”非常有用于对齐RelativeLayout中的所有视图;因此,您可以将button2的左侧与button1的右侧对齐...简单...不需要LinearLayout ...
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@+id/button"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_weight="1"
android:layout_alignParentLeft="false"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_alignTop="@+id/button2" />
#4
0
My goal is to have those buttons side by side but filling the screen width.
我的目标是将这些按钮并排放置,但填充屏幕宽度。
If that's what you want , use a LinearLayout
with android:orientation="horizontal"
and for buttons use android:layout_width="0dp"
, android:weight="1"
in order to take up exactly equal space for each button.
如果这是你想要的,请使用带有android:orientation =“horizontal”的LinearLayout,对于按钮使用android:layout_width =“0dp”,android:weight =“1”,以便为每个按钮占用完全相等的空间。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_weight="1"
android:layout_marginTop="77dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
#5
0
Try this.
尝试这个。
<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="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_marginTop="77dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</TableRow>
</RelativeLayout>
#1
7
Put your Buttons in a LinearLayout
which has horizontal orientation. And assign weight to your buttons.
将您的按钮放在具有水平方向的LinearLayout中。并为您的按钮分配重量。
Here's your code but modified.
这是您的代码,但已修改。
<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">\
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_weight="0.5"
android:text="Left Button"
/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Right Button"
/>
</LinearLayout>
#2
7
<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/dummyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/dummyView"
android:text="Left Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/dummyView"
android:text="Right Button" />
#3
0
It's very simple, if you see in the palette for the buttons you can see the attribute: "layout:alignComponent" very useful to align all views in a RelativeLayout; So you can align a left side of button2 to right side of button1... Simple... No LinearLayout needed...
这很简单,如果你在调色板中看到按钮,你可以看到属性:“layout:alignComponent”非常有用于对齐RelativeLayout中的所有视图;因此,您可以将button2的左侧与button1的右侧对齐...简单...不需要LinearLayout ...
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@+id/button"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_weight="1"
android:layout_alignParentLeft="false"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_alignTop="@+id/button2" />
#4
0
My goal is to have those buttons side by side but filling the screen width.
我的目标是将这些按钮并排放置,但填充屏幕宽度。
If that's what you want , use a LinearLayout
with android:orientation="horizontal"
and for buttons use android:layout_width="0dp"
, android:weight="1"
in order to take up exactly equal space for each button.
如果这是你想要的,请使用带有android:orientation =“horizontal”的LinearLayout,对于按钮使用android:layout_width =“0dp”,android:weight =“1”,以便为每个按钮占用完全相等的空间。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_weight="1"
android:layout_marginTop="77dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
#5
0
Try this.
尝试这个。
<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="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Left Button"
android:id="@+id/button"
android:layout_alignParentTop="false"
android:layout_marginTop="77dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right Button"
android:id="@+id/button2"
android:layout_weight="1"
android:layout_toRightOf="@id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</TableRow>
</RelativeLayout>