沉浸式状态栏时和CoordinatorLayout一起时的坑

公司项目有新的修改,为实现界面滚动时头部工具栏隐藏效果,采用了
CoordinatorLayout 
AppBarLayout
NestedScrollView

等控件

布局文件如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/llayout_appinfo_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/action_bar"
            style="@style/title"
            layout="@layout/include_action_bar" />

        <View style="@style/top_gradient" />
    LinearLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/root_layout"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="match_parent"
        android:layout_below="@id/llayout_appinfo_title"

      >

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"

            android:fitsSystemWindows="true"
            android:paddingTop="0dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:collapsedTitleTextAppearance="@style/ToolBarTitleText"

                app:contentScrim="#46a8ba"
                app:expandedTitleMarginEnd="48dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <include
                    android:id="@+id/head_layout"
                    layout="@layout/include_appinfo_icon"
                    app:layout_collapseMode="pin"
                    app:layout_collapseParallaxMultiplier="0.7" />

            android.support.design.widget.CollapsingToolbarLayout>

            <android.support.design.widget.TabLayout
                android:id="@+id/toolbar_tab"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                android:background="#ffffff"
                android:fillViewport="false"
                app:layout_scrollFlags="scroll"
                app:tabIndicatorColor="@color/theme_color"
                app:tabIndicatorHeight="3dp"
                app:tabSelectedTextColor="@color/theme_color"
                app:tabTextColor="@color/appinfo_text_color">


            android.support.design.widget.TabLayout>

        android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:scrollbars="none"
            android:id="@+id/nsv_viewpager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                  />

        android.support.v4.widget.NestedScrollView>
    android.support.design.widget.CoordinatorLayout>

    <com.tcl.live.widget.ProgressView
        android:id="@+id/progress_view"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:id="@+id/rlayout_translate"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transfer_black_percent_54"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/translate_progressbar"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_centerInParent="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_below="@id/translate_progressbar"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/on_translate"
            android:textColor="#adb0b0"
            android:textSize="20sp" />
    RelativeLayout>

    <include
        android:id="@+id/layout_install"
        layout="@layout/include_install_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

    <RelativeLayout
        android:id="@+id/comment_btn"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        style="@style/MaterialBackground"
        android:background="@color/background">
        <View
            android:id="@+id/top_shadow_bg"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_alignParentTop="true"
            android:background="@drawable/top_shadow" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_toLeftOf="@+id/imageView"
            android:layout_toStartOf="@+id/imageView"
            android:background="@drawable/tv_bg_bottom_line"
            android:gravity="center_vertical|left"
            android:textColorHint="@color/transfer_black_percent_54"
            android:hint="@string/please_input_comment"
            android:textSize="14sp" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="16dp"
            android:src="@drawable/btn_to_comment" />
    RelativeLayout>
RelativeLayout>

在activity中添加函

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        getWindow().setStatusBarColor(getResources().getColor(android.R.color.transparent));
    }
实现沉浸式状态栏结果在 CoordinatorLayout 中出现了空白区域,并且AppBarlayout中出现子控件被挤占现象

解决方法是

if (statusBarHeight >= 0) {
    int sumHeight = statusBarHeight + DeviceManager.dip2px(this, 48);

    View titleView = findViewById(R.id.action_bar);
    LinearLayout.LayoutParams lLayoutParams = (LinearLayout.LayoutParams) titleView.getLayoutParams();
    lLayoutParams.height = sumHeight;
    titleView.setLayoutParams(lLayoutParams);
    View scrollerLayout = findViewById(R.id.collapsing_toolbar_layout);
    scrollerLayout.setPadding(scrollerLayout.getPaddingLeft(),
            statusBarHeight,
            scrollerLayout.getPaddingRight(),
            scrollerLayout.getPaddingBottom());
    View rootlayout = findViewById(R.id.root_layout);
    rootlayout.setPadding(0, -statusBarHeight, 0, 0);
    View titleBarTopView = findViewById(R.id.toolbar_top);
    titleBarTopView.setVisibility(View.VISIBLE);
}

你可能感兴趣的:(android,坑)