【问题标题】:Content behind Toolbar (Android)工具栏背后的内容 (Android)
【发布时间】: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。也就是说,AppBarLayoutRecyclerViewFloatingActionButton 都应该在CoordinatorLayout 内。然后appbar_scrolling_view_behavior 将按预期工作。 FrameLayout 不是必需的。
  • Mike,我认为 FrameLayout 是 NavigationDrawer 工作所必需的。没有它还能用吗?
  • 是的,只要你在DrawerLayout中只有一个主要内容View,它几乎可以是你想要的任何东西。 CoordinatorLayout 和那里的 FrameLayout 一样好用。

标签: android xml


【解决方案1】:

在你的 RecyclerView xml 布局中添加

android.support.v7.widget.RecyclerView
android:clipToPadding="false"
android:paddingTop="56dp"
android:paddingBottom="56dp

检查问题: Android what does the clipToPadding Attribute do?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多