android - 应用栏覆盖 fragment

标签 android xml android-fragments android-toolbar

我正在使用抽屉导航模板构建一个 Android 应用程序。我有一个名为 MainActivity 的类,其布局包含我的 app_bar_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.erik.fuelbuddy.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/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" >

</FrameLayout>

<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:src="@android:drawable/ic_dialog_email" />

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

fragment_container 将用于显示 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="com.erik.fuelbuddy.NearbyFragment"
>

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nearby" />

</FrameLayout>

但是,TextView 显示在工具栏下方,可以通过向其添加填充来确认,例如 100dp。

为什么会这样,我该如何解决?

最佳答案

我通过添加这一行解决了这个问题:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

像这样添加到容器 FrameLayout 中:

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

</FrameLayout>

关于android - 应用栏覆盖 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38064717/

相关文章:

android - 如何使用约束布局拆分两种背景颜色?

java - Android 上的伍德斯托克斯

javascript - 如何获取React Native GoogleSignIn的accessToken?

android - 为什么生成 LiveData 或 Flow 的函数不必从 CoroutineScope 调用?

android - 使用应用程序签名作为 key ?

android - 使用 SupportMapFragment 而不是 MapFragment

android - 如何在底部导航中使用设置屏幕外页面限制

Android - IntelliJ 使用什么 debug.keystore 文件签署我的应用程序?

xml - 使用 ws 寻址从 Web 服务中检索数据

android - 如何在Android中的 fragment 中加载json数据