对齐左/中/右 LinearLayout - Android [英] Aligning Left/Center/Right LinearLayout - Android

查看:31
本文介绍了对齐左/中/右 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 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

好的,如果我理解正确的话:您需要一个由 Scroll View 和屏幕底部的 View 组成的布局,其中有两个按钮,一个与屏幕左侧对齐,另一个与右侧对齐.我认为以下布局建议可以为您解决.尽管如此,如果没有看到您当前的布局,我无法清楚地了解您希望您的布局如何.

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"/>

我添加了 marginLeftmarginRight 以便按钮之间有距离,因为我认为这就是您想要的.

I've added the marginLeft and marginRightso that the buttons have a distance between them, as I think this is what you want.

这篇关于对齐左/中/右 LinearLayout - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆