沉浸式状态栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
int uiVisibility = window.getDecorView().getSystemUiVisibility();
uiVisibility |= flags;
window.getDecorView().setSystemUiVisibility(uiVisibility);
刘海区黑边(内容区域下挫)问题
让内容区域延伸到刘海区
WindowManager.LayoutParams params = window.getAttributes();
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(params);
避开刘海区
TextView topView = findViewById(R.id.top_view);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) topView.getLayoutParams();
layoutParams.topMargin = heightForDisplayCutout();
topView.setLayoutParams(layoutParams);
RelativeLayout containerView = findViewById(R.id.container);
containerView.setPadding(
containerView.getPaddingLeft(),
heightForDisplayCutout(),
containerView.getPaddingRight(),
containerView.getPaddingBottom());
适配刘海屏
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
boolean hasDisplayCutout = hasDisplayCutout(window);
if (hasDisplayCutout){
WindowManager.LayoutParams params = window.getAttributes();
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(params);
int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
int uiVisibility = window.getDecorView().getSystemUiVisibility();
uiVisibility |= flags;
window.getDecorView().setSystemUiVisibility(uiVisibility);
}
setContentView(R.layout.layout_percent);
TextView topView = findViewById(R.id.top_view);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) topView.getLayoutParams();
layoutParams.topMargin = heightForDisplayCutout();
topView.setLayoutParams(layoutParams);
RelativeLayout containerView = findViewById(R.id.container);
containerView.setPadding(
containerView.getPaddingLeft(),
heightForDisplayCutout(),
containerView.getPaddingRight(),
containerView.getPaddingBottom());
}
@RequiresApi(api = Build.VERSION_CODES.P)
private boolean hasDisplayCutout(Window window){
WindowInsets insets = window.getDecorView().getRootWindowInsets();
DisplayCutout displayCutout;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && insets!=null){
displayCutout = insets.getDisplayCutout();
if (displayCutout!=null){
if (displayCutout.getBoundingRects() !=null && displayCutout.getBoundingRects().size()>0 && displayCutout.getSafeInsetTop() > 0){
return true;
}
}
}
return false;
}
private int heightForDisplayCutout(){
int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId>0){
return getResources().getDimensionPixelSize(resId);
}
return 96;
}
|