在ViewPager中的Fragment之间切换时,如何在Android中切换状态栏透明度?

5

我已经研究了与以编程方式更改状态栏颜色相关的所有文章,但没有一个触及到这个特定案例。

我有一个包含ViewPager和BottomNavigationView的Activity。ViewPager包含三个Fragments,而BottomNavigationView在它们之间移动时关闭平滑滚动。

由于三个Fragments中的第一个是地图,当显示第一个Fragment时,我希望状态栏是透明的,但在其他两个Fragments上恢复为不透明的颜色。

以下是我尝试设置和恢复状态栏状态的代码。

private class TabSelectedObserver implements Observer<Integer> {
    @Override
    public void onChanged(@Nullable Integer selectedTab) {
        if (selectedTab != null) {
            activityMainBinding.mainPager.setCurrentItem(selectedTab, false);
            switch (selectedTab) {
                case 0:
                    applyTransparentStatusBarTheme();
                    break;
                default:
                    applyOpaqueStatusBarTheme();
                    break;
            }
        }
    }
}

private void applyTransparentStatusBarTheme() {
    hideSystemUi();
    getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}

private void applyOpaqueStatusBarTheme() {
    showSystemUi();
    getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.rippallete_700));
}

private void hideSystemUi() {
    // Set flags for hiding status bar and navigation bar

    mSystemUiVisibility = mSystemUiVisibility
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
    getWindow().getDecorView().setSystemUiVisibility(mSystemUiVisibility);
}

private void showSystemUi() {
    // Reset flags for hiding status bar and navigation bar
    mSystemUiVisibility = mSystemUiVisibility
            & ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
    getWindow().getDecorView().setSystemUiVisibility(mSystemUiVisibility);
}

这里是一个gif,展示了我目前的问题

我尝试使用fitSystemWindows,但没有得到想要的结果。

如果有人有关于如何为每个片段设置不同的状态栏颜色的建议,其中1为透明,请告诉我,谢谢。

1个回答

0
假设您正在根据所选选项卡的位置设置状态栏颜色,您可以执行类似于以下内容的操作:
private void updateStatusBarColour(int tabPosition) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        switch (tabPosition) {
            case FIRST_TAB:
            case SECOND_TAB:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mRootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
                window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryLight));
                break;
            case THIRD_TAB:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mRootView.setSystemUiVisibility(0);
                }
                window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
                break;
            case FOURTH_TAB:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mRootView.setSystemUiVisibility(0);
                }
                window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorDarkTan));
                break;
        }
    }
}

请注意,我在mRootView上调用setSystemUiVisibility,其中mRootView是包含ViewPager(或您所包含的Activity的根布局)的根父视图。该调用特别设置状态栏的主题,以便状态栏项目(如电池符号)可以在您最终设置的颜色是深色还是浅色背景上可见。

1
我怎样才能将它重置回原始状态? - Nouman Ch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接