对齐左/中/右 LinearLayout - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25587784/
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
Aligning Left/Center/Right LinearLayout - Android
提问by Menelaos Kotsollaris
This should be pretty easy, but since I am new in android development I am asking. I basicaly want to create a static menu bar at the bottom of my avtivity with simple buttons (previous / home / next). However I have problem in aligning those since android:layout_alignParentRight="true"does not seem to do the work. I have also tried out gravity, but neither that works.My current code:
这应该很容易,但由于我是 android 开发的新手,所以我在问。我基本上想用简单的按钮(上一个/主页/下一个)在我的活动底部创建一个静态菜单栏。但是我在对齐这些时遇到了问题,因为android:layout_alignParentRight="true"似乎没有完成这项工作。我也试过重力,但都不起作用。我目前的代码:
<LinearLayout
android:id="@+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/previousButton"
android:layout_alignBottom="@+id/bottomBar"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/nextButton"
android:layout_alignBottom="@+id/bottomBar"
/>
</LinearLayout>
EDIT: here is all my layout xml file:
编辑:这是我所有的布局 xml 文件:
<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:id="@+id/container"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="menelaos.example.com.geolearning.QuizActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/quizTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/answerTableLayout"
android:layout_below="@+id/quizTableLayout">
</TableLayout>
</LinearLayout> </ScrollView>
<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:id="@+id/bottomBar">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<Button
android:id="@+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/nextButton"
android:layout_toRightOf="@id/previousButton"/>
</LinearLayout>
</RelativeLayout>
回答by LuisF
You have to use a RelativeLayout.
您必须使用RelativeLayout。
Try something like 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" >
<Button
android:id="@+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="New Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_alignParentBottom="true"
android:id="@+id/nextButton"
android:layout_toRightOf="@id/previousButton"
/>
</RelativeLayout>
EDIT:
编辑:
try this then:
然后试试这个:
<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:id="@+id/bottomBar">
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<Button
android:id="@+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/nextButton"
android:layout_toRightOf="@id/previousButton"/>
</LinearLayout>
</RelativeLayout>
EDIT 2
编辑 2
Ok, so if I understood correctly: You want a Layout composed by a Scroll View and a View in the bottom of the screen with two buttons, one aligned to the left of the screen and the other aligned to the right. I think the following layout suggestion can work out for you. Nevertheless, without seeing your current layout I cannot clearly understand how you want your layout to be.
好的,如果我理解正确的话:您需要一个由 Scroll View 和屏幕底部的 View 组成的布局,其中有两个按钮,一个与屏幕左侧对齐,另一个与右侧对齐。我认为以下布局建议可以为您解决。尽管如此,如果没有看到您当前的布局,我无法清楚地了解您希望您的布局如何。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="420dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="New Button"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
EDIT 3
编辑 3
In order to have a 3rd button just add the XML below after the last Button view:
为了有第三个按钮,只需在最后一个按钮视图之后添加下面的 XML:
<Button
android:id="@+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center"
android:layout_toRightOf="@id/previousButton"
android:layout_toLeftOf="@id/nextButton"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"/>
I've added the marginLeft
and marginRight
so that the buttons have a distance between them, as I think this is what you want.
我添加了marginLeft
andmarginRight
以便按钮之间有距离,因为我认为这就是您想要的。
回答by Garytech
you can't use the xml attribute alignParentBottom in a LinearLayout. It works for a RelativeLayout.
您不能在 LinearLayout 中使用 xml 属性 alignParentBottom。它适用于RelativeLayout。
Secondly, the attribute gravity only position the content of the tag ( it will put the buttons in the center of the page verticalty)
其次,属性gravity只定位标签的内容(它会将按钮垂直放置在页面的中心)
Try this
尝试这个
<RelativeLayout
android:id="@+id/bottomBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/previousButton"
android:layout_alignBottom="@+id/bottomBar"
android:layout_alignParentLeft="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/nextButton"
android:layout_alignBottom="@+id/bottomBar"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
回答by sergiomse
If you only need to make each button take the same width and occupy the total width of the parent layout:
如果只需要让每个按钮的宽度相同并占据父布局的总宽度:
In buttons, change to android:layout_width="0dp"
and add android:layout_weight="1"
在按钮中,更改为android:layout_width="0dp"
并添加android:layout_weight="1"
This will distribute all the buttons with homogeneous width.
这将分布具有均匀宽度的所有按钮。