我尝试创建一个应用。该应用程序的唯一一个片段具有透明的工具栏和状态栏。
我必须以编程方式完成此任务。我写下面的代码来安排状态栏的透明度:
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函数。
这不是黄色!如何安排基于片段的状态栏透明度?