Android-设置透明状态栏

时间:2018-10-22 22:03:33

标签: android android-statusbar android-framelayout

我尝试创建一个应用。该应用程序的唯一一个片段具有透明的工具栏和状态栏。

必须以编程方式完成此任务。我写下面的代码来安排状态栏的透明度:

void translucentStatusBar(Activity activity) {
    Window window = activity.getWindow();
    int flags = window.getDecorView().getSystemUiVisibility();
    flags |= WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
    flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    window.addFlags(flags);

    // ID_ANDROID_CONTENT = The ID that the main layout in the XML layout file should have.
    ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
        mChildView.setFitsSystemWindows(false);
        ViewCompat.requestApplyInsets(mChildView);
    }
}

void revertTranslucentStatusBar(Activity activity) {
    Window window = activity.getWindow();
    int flags = window.getDecorView().getSystemUiVisibility();
    flags &= ~WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
    flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

    window.clearFlags(flags);
    // ID_ANDROID_CONTENT = The ID that the main layout in the XML layout file should have.
    ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
        mChildView.setFitsSystemWindows(true);
        ViewCompat.requestApplyInsets(mChildView);
    }
    if(Build.VERSION.SDK_INT >= 21)
        window.setStatusBarColor(Color.YELLOW);
}

Fragment1 onViewCreated调用translucentStatusBar函数,下一页是Fragment2。 Fragment1没问题。

Fragment2的onViewCreated调用revertTranslucentStatusBar函数。

片段1 enter image description here

Fragment2 enter image description here

这不是黄色!如何安排基于片段的状态栏透明度?

0 个答案:

没有答案