对齐左/中/右LinearLayout-Android [英] Aligning Left/Center/Right LinearLayout - Android
问题描述
这应该很容易,但是由于我是android开发的新手,所以我要问.我基本上想在我的活动的底部使用简单的按钮(上一个/主页/下一个)创建一个静态菜单栏.但是由于 android:layout_alignParentRight ="true" 似乎没有用,所以我在对齐它们时遇到了问题.我也尝试过引力,但都行不通.我当前的代码:
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:
<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>
这是我所有的布局xml文件:
here is all my layout xml file:
<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>
推荐答案
您必须使用 RelativeLayout .
尝试这样的事情:
<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>
然后尝试:
<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>
编辑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.
<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>
编辑3
要拥有第三个按钮,只需在最后一个按钮"视图之后添加以下XML:
In order to have a 3rd button just add the XML below after the last Button view:
<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"/>
我添加了 marginLeft
和 marginRight
,以便按钮之间保持一定的距离,因为我认为这就是您想要的.
I've added the marginLeft
and marginRight
so that the buttons have a distance between them, as I think this is what you want.
这篇关于对齐左/中/右LinearLayout-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!