10

I am develop a screen with CollapsingToolbarLayout.

When I use this layout in Activity. Everything is okay.

But when I use it in Fragment. The Toolbar are under status bar.

In activity

In Fragment

This is my code. Please help me. Thank you.

<?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"
    >
  <!--App Bar Layout-->
  <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar_layout"
      android:layout_width="match_parent"
      android:layout_height="300dp"
      android:fitsSystemWindows="true"
      >
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsed_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
    app:contentScrim="?attr/colorPrimary"
    >
  <android.support.v4.view.ViewPager
      android:id="@+id/view_pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      app:layout_collapseMode="parallax"/>
  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      app:layout_collapseMode="parallax"
      android:src="@mipmap/ic_launcher"
      />
  <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      app:layout_collapseMode="pin"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
      >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lay_topbar"
        >
      <ImageView
          android:id="@+id/img_logo"
          android:layout_width="44dp"
          android:layout_height="44dp"
          android:background="#FF0000"
          android:layout_centerVertical="true"
          />
      <ImageButton
          android:id="@+id/imb_home"
          android:layout_width="44dp"
          android:layout_height="44dp"
          android:background="#00FF00"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          />
      <ImageButton
          android:id="@+id/imb_cart"
          android:layout_width="44dp"
          android:layout_height="44dp"
          android:background="#0000FF"
          android:layout_toLeftOf="@id/imb_home"
          android:layout_centerVertical="true"
          />
    </RelativeLayout>
  </android.support.v7.widget.Toolbar>

</android.support.design.widget.CollapsingToolbarLayout>
Duong Nguyen
  • 172
  • 1
  • 9

2 Answers2

14

I had the same issue. Put:

android:fitsSystemWindows="true"

in the root of your Activity's layout(the Activity that contains the fragment), not in the root of the fragment's layout(in your case the CoordinatorLayout).

Boken
  • 4,825
  • 10
  • 32
  • 42
Davide Armagni
  • 156
  • 1
  • 5
  • Davide! Thank you for setting me on the right track. In my case, I didn't put it in my activity's root, but I put it in the container element that I attach my fragment to. – DWndrer Sep 20 '18 at 11:47
0

Try adding below statement in the CollapsingToolbarLayout.

android:fitsSystemWindows="true"
Boken
  • 4,825
  • 10
  • 32
  • 42
Sanjog Shrestha
  • 417
  • 9
  • 16