【问题标题】:Android DrawerLayout,CoordinatorLayout and AppBarLayout white space under AppBarLayoutAndroid DrawerLayout,CoordinatorLayout和AppBarLayout下AppBarLayout留白
【发布时间】:2016-05-04 09:47:04
【问题描述】:

我有下面的 XML 布局,一切都按预期工作,但问题是 AppBarLayout 和 Recyclerview 之间存在空白。 我做错了什么或我错过了什么。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabIndicatorHeight="2dp"
                app:tabMode="fixed" />

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    <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_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

提前致谢。

【问题讨论】:

  • 我记得这也发生在我身上,虽然我不记得我是如何解决的,你可以先尝试从AppBarLayout和Coordinator布局中删除android:fitsSystemWindows="true"
  • @varunkr 感谢您的重播,我试过了,效果很好,但这会使导航抽屉在打开/关闭时变慢。
  • @ThairJaber 在下面看到我的回答。
  • @ThairJaber 你检查了吗?
  • @Harshad 似乎不起作用,他的导航抽屉在打开和关闭时似乎仍然很慢

标签: android xml material-design


【解决方案1】:

尝试在 RecyclerView 中使用以下代码。

android:layout_gravity="fill_vertical" 

【讨论】:

    【解决方案2】:

    我只是面临同样的问题。似乎这是为上部指示条保留的空间。 目前,我已经在 DrawerLayout 本身中使用 android:fitSystemWindows="false" 解决了,并且在第一个布局内容中(这很重要,您必须更改两者)。

    activity_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="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.b_thinking.test.MainActivity"
        >
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="false"
            android:theme="@style/AppTheme.AppBarOverlay"
            >
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                />
    
            <android.support.v4.widget.DrawerLayout
                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:id="@+id/drawer_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="false"
                tools:openDrawer="start"
                >
    
                <include layout="@layout/content_main"/>
    
                <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="false"
                    app:headerLayout="@layout/nav_header_main"
                    app:menu="@menu/activity_main_drawer"
                    />
            </android.support.v4.widget.DrawerLayout>
    
        </android.support.design.widget.AppBarLayout>
    
    
        <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"
            app:srcCompat="@android:drawable/ic_dialog_email"
            />
    
    </android.support.design.widget.CoordinatorLayout>
    

    content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.b_thinking.test.MainActivity"
        tools:showIn="@layout/activity_main"
        android:fitsSystemWindows="false"
        >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textColor="@color/colorAccent"
            />
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 2015-12-07
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多