Transparent StatusBar when collapsed

南楼画角 提交于 2019-12-11 12:44:45

问题


I have three working status bars. But when the CollapsingToolbar is collapsed, a status bar remains transparent.
I want the status bar to have the PrimaryDark color if collapsed.

How do I detect if the status bar is collapsed and remove the transparency?

Print – Expanded:


Print – Collapsed:


XML Code:

<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>

回答1:


try this

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);
                }
            }
        });


来源:https://stackoverflow.com/questions/46981038/transparent-statusbar-when-collapsed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!