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

时间:2010-09-27 09:04:26

标签: android

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

** Thanx提前

5 个答案:

答案 0 :(得分:13)

我也在寻找这一段时间。我刚刚找到的解决方案是使用属性:

android:layout_alignParentBottom="true"

而不是您现有的android:layout_gravity="bottom"

不幸的是,我的布局仅在其他 RelativeLayout作为根布局时添加,其余部分添加到其中。也许情况并非总是这样,但是有一个LinearLayout(即使被告知使用fill_parent)它只使用所有添加元素所需的高度,并将我的页脚放在那些底部。

我来到这个解决方案,看看这个相关的问题:How do I achieve the following result using 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>

答案 1 :(得分:11)

底部所需的布局应具有:
android:gravity = "bottom"
它的父母应该有:
android:layout_height="fill_parent"

答案 2 :(得分:1)

你应该在最后一个布局前放置一个视图,重量为1,它会把它放到最底层。像这样:

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

答案 3 :(得分: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>

It produces something that looks like this

答案 4 :(得分:0)

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