Android Content Moves Up When Hiding ActionBar

时间:2018-12-19 11:38:59

标签: android android-fragments layout

My app has a Toolbar within an AppBarLayout that disappears on scroll, using the app:layout_scrollFlags="scroll|enterAlways" tag. When I replace the current Fragment with one that does not need (or should not have) a toolbar, the following issue occurs.

I'm using ((AppCompatActivity) getActivity()).getSupportActionBar().hide(); to hide the ActionBar in that Fragment. If the ActionBar was visible in the previous Fragment, everything works as expected. However, if the ActionBar was not visible in the previous Fragment due to the scroll|enterAlways configuration, the content in the following Fragment that hides the ActionBar is pushed upwards behind the status bar. The following images should show what I mean:

This is how it should look like. Occurs when the ActionBar was not visible in the previous Fragment. This is how it should look like. Occurs when the ActionBar was not visible in the previous Fragment.

This is how it looks, if the ActionBar was previously invisible. This is how it looks, if the ActionBar was previously invisible.

For reference, this is how my Activity.xml looks like:

<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/white"
tools:context="com.example.app.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        app:contentInsetLeft="15dp"
        app:contentInsetStart="15dp"
        app:contentInsetEnd="25dp"
        app:contentInsetRight="25dp"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/currentFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    >
</FrameLayout>

and this is how the Fragment.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.ModuleDetailFragment">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/moduleImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:maxHeight="250dp"
        android:adjustViewBounds="true"
        />
...

0 个答案:

没有答案