简体   繁体   中英

ViewPager set to the same height regardless of immersive (full screen) mode on/off

I have a ViewPager inside a CoordinatorLayout like so (AppBarLayout is simplified for clarity):

<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MyActivity">

<android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.Toolbar>
        <Spinner/>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

In MyActivity I enable full screen right after calling super.onCreate() like this:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
            );
        } else {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

Later, when the ViewPager is drawn on the screen I call ViewPager.getHeight(). I expect the height of the viewPager to differ based on whether I execute the full screen code above, but I find that this is not the case. I am getting the same height for the viewPager regardless of whether the activity is in full screen (immersive) or not. Am I missing something?

Turns out this was due to having

    android:fitsSystemWindows="true"

in my layout xml. Reading the documentation I think I'm still not sure I 100% understand what this flag does though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM