将片段对齐到屏幕底部

6

你好,我有一个线性布局,里面放了一个片段小部件。我想把这个片段放在屏幕底部。因为以后页面可能会可滚动,所以我希望它始终锚定在屏幕底部。我该如何实现呢?


那么问题是什么? - KOTIOS
哦,太好了,我应该得到点赞和接受答案的评价 :P 不是吗? - KOTIOS
2个回答

6
您必须使用RelativeLayout。类似这样的代码:
<?xml version="1.0" encoding="utf-8"?>
<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" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_fragment" >
    </ScrollView>

    <fragment
        android:id="@+id/bottom_fragment"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true" >
    </fragment>

</RelativeLayout>

0
要使用约束布局实现这一点,请使用下面的代码行。

app:layout_constraintBottom_toBottomOf="parent"

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:paddingBottom="@dimen/padding_16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:paddingTop="@dimen/padding_16dp">
        //UI Components..
</android.support.constraint.ConstraintLayout>

看起来像是bottom_sheet_fragment。这是xml文件的根标签。我告诉ConstraintLayout,它的底部应该停留在父级(activity)的底部,并且高度属性应该包裹片段内的其他UI组件,然后它就会粘在屏幕的父级底部。


1
你能再解释一下吗? - Dieter Meemken
这个答案有点含糊,请再解释清楚一些。 - Eman

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