【问题标题】:App bar covers fragment应用栏覆盖片段
【发布时间】:2016-06-27 23:21:10
【问题描述】:

我正在使用导航抽屉模板构建一个 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 将用于显示片段。下面以其中一个为例:

<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 显示在 ToolBar 下方,可以通过向其添加填充来确认,例如 100dp。

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

【问题讨论】:

    标签: android xml android-fragments android-toolbar


    【解决方案1】:

    我通过添加这一行来解决它:

    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>
    

    【讨论】:

      【解决方案2】:

      尝试在添加内容之间添加布局

      意味着您直接显示工具栏,即工具栏直接包含完整内容视图布局,因此您应该在工具栏和容器之间进行布局。

      像这样显示你想要的布局但是要显示内容视图的布局

          <?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">
      
      
      <LinearLayout>
      android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
      
      
      <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" />
      </LinearLayout>
      
      
      
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        【解决方案3】:

        我刚遇到这个问题,试试这个: 从您的主布局容器中删除这一行 android:fitsSystemWindows="true",在您的情况下为layapp_bar_main.xml,它适用于我

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-06
          • 2022-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-18
          相关资源
          最近更新 更多