android - fragment 内容在抽屉导航中的 ActionBar 上重叠

标签 android android-fragments android-actionbar navigation-drawer

为什么 Fragment 布局会像这样与 ActionBar 重叠

  1. 第一个 fragment

    <FrameLayout 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=".FirstFragment">
    
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_first_fragment" />
    

    enter image description here

  2. 第二个 fragment

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_second_fragment" />
    

    enter image description here

activity_navigation

<?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_navigation"
        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:headerLayout="@layout/nav_header_navigation"
        app:menu="@menu/activity_navigation_drawer" />

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

app_bar_navigation

<?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.sup.slideup.Navigation">

    <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/main_fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

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

使用 KitKat 设备。

最佳答案

如果您想在 Activity 布局中保持非 LinearLayout,您需要在包含您的 FragmentFrameLayout 上设置一个 layout_marginTop .在您的 app_bar_navigation.xml 中:

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

<FrameLayout
    android:id="@+id/main_fragment_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_marginTop="?attr/actionBarSize"
    />
...

关于android - fragment 内容在抽屉导航中的 ActionBar 上重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115532/

相关文章:

java - SharedPreferences 不想存储我的代码

java - 为什么 java/android 需要在 XML 元素的开头有一个空格来解析它?

android actionbar oncreateoptionsmenu 未调用

java - CoordinatorLayout 内的 RecyclerView 内的水平 RecyclerView

android - 具有不同页面布局的 viewpager 的操作栏选项卡

java - 我的应用程序有一个操作栏,但没有 menu.xml。如何修改我的操作栏?

android - ActionBarStyleGenerator - 如何使用它?

android - 具有相对输出路径的 greenDAO 模式生成;未找到 i/o 失败

java - 覆盖函数的 Android Activity 必须调用 super.*

android - 如何在 Fragment 上使用 android.hardware.camera2?