【问题标题】:Navigation Drawer Fragment cut off at top顶部截断的导航抽屉片段
【发布时间】:2016-08-20 03:14:02
【问题描述】:

我在 android 上使用 Navigation Drawer 模板,我也在使用片段。只要有片段,该动作就会切断片段的顶部。除了添加片段外,我没有更改默认代码。如何让片段加载到操作栏下方?

问题图片:

*更新 现在看起来像这样

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"
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">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


<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:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

片段 xml

<LinearLayout 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"
tools:context="com.example.android.slidemenu.TestFragment"
android:orientation="horizontal">

<!-- TODO: Update blank fragment layout -->
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@android:drawable/ic_menu_compass"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TESTTEST"
    android:textSize="50sp"/>
</LinearLayout>

应用栏主 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.example.android.slidemenu.MainActivity">

<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="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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


<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

【问题讨论】:

标签: android android-fragments navigation-drawer


【解决方案1】:

FrameLayout 必须位于Toolbar 之下,并位于CoordinatorLayout 之内。尝试像这样进行重组。

DrawerLayout 中包含“主要内容”。然后在AppBarLayout 之后添加FrameLayout(如果不需要片段,则包括其他视图)。

“推”下 FrameLayout 的秘密线就是这条线

app:layout_behavior="@string/appbar_scrolling_view_behavior"

抽屉布局 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"
    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">

    <include
        layout="@layout/nav_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:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

nav_content.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.example.android.slidemenu.MainActivity">

    <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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

【讨论】:

  • 我在你说的地方添加了framelayout,它解决了片段在操作栏顶部但片段顶部仍然被阻塞的问题。
  • @T.Yang 您可以将 AppBarLayout 和 FrameLayout 包装在 Vertical LinearLayout 中,这样它们就不会重叠
  • 更新的答案和包装布局都有效
  • 如果你有一个折叠工具栏,这个答案更受欢迎
【解决方案2】:

FrameLayout 放在CoordinatorLayout 中,放在AppBarLayout 之后。

编辑:尝试删除android:fitsSystemWindows=true

【讨论】:

  • 没有。改变了答案。
  • fragment的顶部还是被挡住了。
  • 还是一样的问题
  • @T.Yang 你删除了它出现的所有实例吗?
【解决方案3】:

android:fitsSystemWindows 设置为false 而不是删除它对我有用。感谢 amitairos 的提示;)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多