Android 9.0 notch screen adaptation

Reference article
Demo address

In full screen

In the full screen state, the status bar does not exist, and the system DecorView布局will not extend to the bangs area by default .

Set full screen
// 去除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
// WindowManager.LayoutParams.FLAG_FULLSCREEN: 让window进行全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Determine whether it is Liu Haiping
private boolean hasDisplayCutout(Window window) {
    
    
    DisplayCutout displayCutout;
    View rootView = window.getDecorView();
    WindowInsets insets = rootView.getRootWindowInsets();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && insets != null) {
    
    
        // 获取显示切口
        displayCutout = insets.getDisplayCutout();
        if (displayCutout != null) {
    
    
            // 如果切口不为null
            if (displayCutout.getBoundingRects() != null && displayCutout.getBoundingRects().size() > 0 && displayCutout.getSafeInsetTop() > 0) {
    
    
                // 1. 如果切口区域集合不为null
                // 2. 切口区域存在
                // 3. 安全区域距离顶部距离不为0
                // 三个条件满足,则屏幕中存在刘海.
                return true;
            }
        }
    }
    return false; 
}
Whether the setting content can be extended to the bangs area
WindowManager.LayoutParams params = window.getAttributes();
// LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT 全屏模式,内容下移,非全屏不受影响
// LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 全屏模式,允许内容区域延伸进刘海区
// LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER 全屏模式,不允许内容区域延伸进刘海区
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(params);
  1. LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULTAnd LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVERnot allowed to extend the bangs effect, the area of ​​the bangs is black.

Insert picture description here

  1. LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGESAllow extension to the bangs effect, the area of ​​the bangs is white.

Insert picture description here

Extend the content to the bangs area

Extending the content to the bangs area requires an immersive status bar. Note that if the content area is set in the previous step to extend the content area to the bangs area, the immersive status bar in this step will not work even if it is set.

// View.SYSTEM_UI_FLAG_FULLSCREEN: 状态栏隐藏
// View.SYSTEM_UI_FLAG_HIDE_NAVIGATION: 导航栏隐藏
// View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN: 视图延伸至状态栏区域,状态栏上浮于视图之上
int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
int visibility = window.getDecorView().getSystemUiVisibility();
visibility |= flags; //追加沉浸式设置
window.getDecorView().setSystemUiVisibility(visibility);

The final effect is:

Insert picture description here

Not full screen

In the non-full screen state, the status bar exists (the height of the bangs area is equal to the height of the status bar by default). At this time, the content area is extended to the bangs area (that is, the status bar area) as follows.

Remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
Set the content area extension to the bangs area
// 设置状态栏颜色透明
getWindow().setStatusBarColor(Color.TRANSPARENT);
// 这个标记的意思是绘制一个背景透明的状态栏,然后用StatusBarColor中的颜色去填充,上面一步已经设置了StatusBarColor的颜色.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// 最后还要清楚清除这个标记
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN: 视图延伸至状态栏区域,状态栏上浮于视图之上
// SYSTEM_UI_FLAG_LIGHT_STATUS_BAR: 设置状态栏中字体与图标颜色为黑色.
int flags =  View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
int visibility = getWindow().getDecorView().getSystemUiVisibility();
visibility |= flags;// 追加属性
getWindow().getDecorView().setSystemUiVisibility(visibility);

The final result is as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/MoLiao2046/article/details/106890287