【问题标题】:Aligning Left/Center/Right LinearLayout - Android对齐左/中/右线性布局 - Android
【发布时间】:2026-02-15 07:40:01
【问题描述】:

这应该很容易,但由于我是 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>

编辑:这是我所有的布局 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>

【问题讨论】:

    标签: android layout android-studio alignment


    【解决方案1】:

    您必须使用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 组成的布局,其中有两个按钮,一个与屏幕左侧对齐,另一个与右侧对齐。 我认为以下布局建议可以为您解决。然而,如果没有看到您当前的布局,我无法清楚地了解您希望您的布局如何。

    <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:

    <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,以便按钮之间有一定距离,因为我认为这就是您想要的。

    【讨论】:

    • 这不会起作用,因为布局不在我想要的包装我的内容的设备底部
    • 我不确定我是否理解......你想要底部的RelativeLayout吗?你会在它上面放置什么布局?我不确定这是否可能,在一个活动中有两个布局......
    • 我的布局上方有一个 ScrollView。这一定是可能的,因为许多应用程序都有这种布局。这是一个程序的链接:youtube.com/watch?v=EI4MP0DpFeE。检查分钟:2:09 底部菜单栏。这就是我想要的
    • 这也行不通,因为 RelativeLayout 不在底部。加上按钮无法对齐,因为我希望它们对齐
    • 您能提供您的布局xml吗?我假设 RelativeLayout 是根布局,而 LinearLayout 是底部的那个
    【解决方案2】:

    您不能在 LinearLayout 中使用 xml 属性 alignParentBottom。它适用于相对布局。

    其次,重力属性只是定位标签的内容(它会将按钮放在页面垂直的中心)

    试试这个

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

    【讨论】:

    • 你的代码抛出异常:“@+id/bottombar 不是同一个RelativeLayout中的兄弟”
    【解决方案3】:

    如果只需要让每个按钮的宽度一样,占据父布局的总宽度:

    在按钮中,更改为android:layout_width="0dp" 并添加android:layout_weight="1"

    这将使所有按钮的宽度均匀分布。

    【讨论】: