如何将相对布局或线性布局放置在屏幕底部?

10

我在xml中有以下内容

我想把第二个线性布局放在屏幕底部。我该怎么做? 我已经将第二个相对布局的属性设置为底部,但仍未显示在底部。

**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">    
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">   
</RelativeLayout>
<RelativeLayout android:id="@+id/RelativeLayout02" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">   
</RelativeLayout>
<RelativeLayout android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:layout_gravity="bottom">
</RelativeLayout>
</LinearLayout>

提前致谢


我想把第二个相对布局放在屏幕底部。 - Sandy
6个回答

20

我也搜索了一段时间。我的解决方案是使用属性:

android:layout_alignParentBottom="true"

与其使用现有的 android:layout_gravity="bottom",我建议您尝试以下解决方案:

但很遗憾,在我的布局中,只有当添加一个额外的RelativeLayout作为根布局,并将其余内容添加到其中时才有效。也许这并不总是这种情况,但是如果使用 LinearLayout(即使告诉它使用fill_parent),它仅使用所添加的所有元素所需的高度,并将我的页脚放置在它们的底部。

我找到了这个解决方案,看着这个相关的问题:如何使用RelativeLayout实现以下结果?

编辑此处因为答案评论中的格式丢失: 您是说您无法重写它还是它行不通?

我也有一个垂直的LinearLayout,我的新xml结构看起来像这样(没有布局大小参数等):

<RelativeLayout>
    <RelativeLayout 
        android:id="@+id/RelativeLayout01">
    </RelativeLayout>
    <RelativeLayout 
        android:id="@+id/RelativeLayout02" 
        android:layout_below="@+id/RelativeLayout01">   
    </RelativeLayout>
    <RelativeLayout 
        android:layout_below="@+id/RelativeLayout02" 
        android:layout_alignParentBottom="true">
    </RelativeLayout>
</RelativeLayout>

你已经尝试过按照我最新示例的方式重写XML了吗?如果还不起作用,也许你可以提供更多关于你想要的布局的细节。 - sunadorer

11
您想要底部的布局应该具有以下内容:
android:gravity = "bottom"
它的父元素应该有:
android:layout_height="fill_parent"

我设置了上述两个属性,但布局仍未在屏幕底部。 - Sandy
父布局完全填充屏幕。子元素具有android:layout_height="wrap_content"。 - Sandy
你能否在问题中发布你的XML,以便我们查看?请点击问题下方的“编辑”按钮并粘贴。 - fredley
我的 XML 和上面的一样。。我找不出错误。你能告诉我我错在哪里吗? - Sandy
我正在使用android:layout_gravity="bottom",这是用于布局的,那么问题出在哪里? - Sandy
显示剩余2条评论

1

在最后一个权重为1的布局之前,应该放置一个视图,并将其放置在底部。就像这样:

    <View android:id="@+id/emptySpace"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

1
<Linearlayout android:layout_height="fill_parent" 
android:orinationtation="vertical"
android:layout_width="fill_parent">

<RelativeLayout  android:layout_height="0dp" 
android:layout_width="fill_parent"
android:weight="1">
</RelativeLayout>

<RelativeLayout 
android:layout_height="0dp" 
android:layout_width="fill_parent"
android:weight="1"
>   
</RelativeLayout>



</Linearlayout >

1

尝试使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#ffb3e5fc"
   android:id="@+id/linearLayout1">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">
<Button
    android:id="@+id/button1"
    android:text="Left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/centerPoint" />
<TextView
    android:id="@+id/centerPoint"
    android:text=""
    android:layout_width="1dip"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />
<Button
    android:id="@+id/button2"
    android:text="Right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/centerPoint" />
</RelativeLayout>
</LinearLayout>

它会生成类似于这样的东西


0
对我来说,这个工作很顺利。
    <RelativeLayout
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#fff"
    android:gravity="bottom"
    app:layout_anchor="@+id/fab_inout"
    app:layout_anchorGravity="bottom|center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsfsdfasdf df" />

</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接