Android 我想把我的相对布局放在底部

Android 我想把我的相对布局放在底部,android,android-layout,Android,Android Layout,我应该做些什么改变以使相对布局位于底部 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="ve

我应该做些什么改变以使相对布局位于底部

      <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="86dp"
    android:layout_alignParentBottom="true"
    android:layout_weight="0.08"
    android:gravity="bottom" >
   </RelativeLayout>
      </LinearLayout>

您可以通过使用
框架布局而不是
线性布局来实现这一点,因为默认情况下,它从上到下堆叠子对象

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:layout_gravity="bottom" >
    </RelativeLayout>

</FrameLayout>

您需要将父布局更改为
相对布局
,因为属性
android:Layout\u alignParentBottom
线性布局
子项没有任何作用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:layout_alignParentBottom="true" >
    </RelativeLayout>
</RelativeLayout>

使用


这里有两个问题。首先是

android:gravity

这是错误的。这将为此
视图的内容设置
重力
。因此,例如在
TextView
中使用,如果
TextView
,它会将
text
放在底部。你想要的是

android:layou重力布局

这将在其父视图中设置
视图
s
重力

第二个问题是,
android:gravity=“bottom”
LinearLayout
中无法像您所期望的那样工作。我不确定我能解释为什么,所以我会尝试找到一个链接来解释它。但是如果您将
线性布局的
方向更改为
水平方向
,您将看到这一点

因此,您最好的选择是更改
方向(如果可能),如果没有,则将根
视图组更改为
相对视图(RelativeLayout),并按照某人的建议使用属性
android:alignParentBottom=“true”

编辑以进行解释


此外,还有其他人对此做了一些解释。但基本上,如果
方向
水平
,则
视图
按预期从左到右排列,因此您无法控制左右
重力
。对于
垂直方向
,对于顶部和底部的
重力
也是如此,因为当您设置
方向时,它们垂直放置的位置已经由Android决定了。我希望这是有意义的。

删除android:layout\u weight=“0.08”,您没有将权重总和分配给其父布局。并使父项相对化。@rahulkapoor
weight\u sum
在使用
weight
时是不必要的。只有在某些情况下,它仍然没有帮助。@AmeerHumza将父项线性布局更改为相对化布局。您是否尝试使用
android:layout\u gravity
而不是
weighty
?框架布局也没有帮助:/检查我编辑的答案。为了做到这一点,你需要“布局\重力”。当用作父对象时,使用框架布局而不是相对布局有什么区别@Gian1200你组织孩子的方式。例如layout\u gravity=“bottom”与layout\u alignParentBottom=“true”。另一件事可能是内存使用,但我还没有做过这样的测试,以知道哪一个更有效。但这两种方法都应该奏效。这取决于代码的其余部分。有些东西使用FrameLayout比使用LinearLayout和viceversa更容易实现。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- replace this by any other layout or view. You might want something here, right? -->    
    <View android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="86dp">
    </RelativeLayout>

</LinearLayout>