Android RecyclerView Gravity问题

时间:2019-06-23 05:57:42

标签: android android-recyclerview recyclerview-layout

我正在使用DrawerLayout来显示Navigation Drawer,其中A NavigationView的{​​{1}}代码是:

MyDrawerLayout.xml

xml

layout_navigation_list.xml

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <include layout="@layout/layout_navigation_list" />

</android.support.design.widget.NavigationView>

ScreenImage

<?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="wrap_content" android:background="@color/white" android:orientation="vertical"> <include android:id="@+id/headerLayout" layout="@layout/nav_header_dash_nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_gravity="top" android:gravity="top" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerNav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/headerLayout" android:layout_alignParentBottom="true" android:layout_gravity="bottom" android:background="@color/white" android:foregroundGravity="bottom" /> </RelativeLayout> 中,只有3个项目需要显示,它们正对齐在RecyclerView布局的顶部,这是我不希望的。因此,我的问题是,如何将这些项目发送到RecyclerView的底部(如图所示)。

3 个答案:

答案 0 :(得分:1)

match_parent中设置wrap_content而不是layout_height字段relativeLayout

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/white"
 android:orientation="vertical">

答案 1 :(得分:0)

插入ConstraintLayout

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:orientation="vertical">

    <include
        android:id="@+id/headerLayout"
        layout="@layout/nav_header_dash_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="top"
        android:gravity="top" />

    <android.support.constraint.ConstraintLayout
        android:layout_below="@id/headerLayout"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerNav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
         />

    </android.support.constraint.ConstraintLayout>

</RelativeLayout>

答案 2 :(得分:0)

在文件MATCH_PARENT的根目录相对布局中,您必须达到WRAP_CONTENT的高度,而不是layout_navigation_list.xml的高度。看起来像是

<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/white"
   android:orientation="vertical">
 ...
 ...
 ...
</RelativeLayout>

注意:重力不适用于相对布局。