【发布时间】:2018-04-22 19:27:28
【问题描述】:
我正在尝试在抽屉布局上添加回收站视图。问题是 RecyclerView 位于单独的文档 content_main.xml 中,位于主文档 activity_main.xml 中的工具栏后面。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--Contents-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--App Bar declaration-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--Content-->
<include
layout="@layout/content_main" />
<!--FAB-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:tint="@color/colorIcons"
app:srcCompat="@android:drawable/ic_input_add" />
</FrameLayout>
<!--NavigationDrawer content-->
<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"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.ezloop.yesmom.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvTaskList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
我尝试添加 this answer 中的 app:layout_behavior="@string/appbar_scrolling_view_behavior",但它不起作用。我还将高度从 match_parent 更改为 wrap_content
【问题讨论】:
-
您需要在 content_main.xml 文件的父视图中添加 android:layout_marginTop="?attr/actionBarSize"。它会解决你的问题。
-
在
CoordinatorLayout中只有一个RecyclerView是没有意义的。相反,CoordinatorLayout应该是外部内容View。也就是说,AppBarLayout、RecyclerView和FloatingActionButton都应该在CoordinatorLayout内。然后appbar_scrolling_view_behavior将按预期工作。FrameLayout不是必需的。 -
Mike,我认为 FrameLayout 是 NavigationDrawer 工作所必需的。没有它还能用吗?
-
是的,只要你在
DrawerLayout中只有一个主要内容View,它几乎可以是你想要的任何东西。CoordinatorLayout和那里的FrameLayout一样好用。