沉浸式实现
沉浸式设置
private void setSystemUIStatus(Window dialogWindow) {
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
dialogWindow.getDecorView().setSystemUiVisibility(View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
dialogWindow.getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
dialogWindow.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
});
dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
dialogWindow.setStatusBarColor(Color.TRANSPARENT);
dialogWindow.setNavigationBarColor(Color.TRANSPARENT);
}
在style文件中设置
<!--沉浸式状态栏并设置为透明-->
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
自适应距离状态栏
//设置自适应距离状态栏
ViewCompat.setOnApplyWindowInsetsListener(layout, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View layout, WindowInsetsCompat insets) {
ConstraintLayout.LayoutParams layoutParams =
(ConstraintLayout.LayoutParams) layout.getLayoutParams();
layoutParams.topMargin = insets.getSystemWindowInsetTop();
return insets;
}
});
Android7.0以上沉浸式状态栏灰色,在设置布局之前添加
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
field.setAccessible(true);
field.setInt(getWindow().getDecorView(), Color.TRANSPARENT); //设置透明
} catch (Exception exception) {
exception.printStackTrace();
}
}