折叠时透明的 StatusBar

Transparent StatusBar when collapsed

我有三个工作状态栏。但是当 CollapsingToolbar 折叠时,状态栏保持透明。
我希望状态栏在折叠时具有 PrimaryDark 颜色。

如何检测状态栏是否折叠并移除透明度?

打印 – 扩展:


打印 – 折叠:


XML代码:

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="#f15b6e"
            app:expandedTitleGravity="bottom"
            android:background="@android:color/white"
            app:expandedTitleMarginBottom="170dp"
            app:expandedTitleMarginStart="10dp"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">




            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="225dp"
                android:fitsSystemWindows="true">

                <ImageView
                    android:id="@+id/main.backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/nav_header_height"
                    android:src="@drawable/hair"
                    app:layout_collapseMode="parallax"
                    android:adjustViewBounds="true"
                    android:fitsSystemWindows="true"
                    android:cropToPadding="false"
                    android:scaleType="fitXY"
                    />

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="225dp"
                    android:background="@drawable/gradient_bg">

</FrameLayout>

试试这个

AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout_edit_profile);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            boolean isShow = false;
            int scrollRange = -1;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (scrollRange == -1) {
                    scrollRange = appBarLayout.getTotalScrollRange();
                }
                if (scrollRange + verticalOffset == 0) {       
                      Window window = activity.getWindow();
                      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

                      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                      window.setStatusBarColor(activity.getResources().getColor(R.color.primary_color));


                    isShow = true;
                } else if (isShow) {
                    collapsingToolbarLayout.setTitle(" ");
                    isShow = false;
                    Window w =PerfilEmpresaActivity.this.getWindow(); 
                    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATU‌​S, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                }
            }
        });