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”,您没有将权重总和分配给其父布局。并使父项相对化。@rahulkapoorweight\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>