Android 9.0 适配——刘海屏

前言

刘海屏是指某些设备显示屏上的一个区域延伸到显示面,这样既能为用户提供全面屏体验,又能为设备正面的重要传感器留出空间。Android在搭载Android 9.0/P(API 级别 28)及更高版本的设备上正式支持刘海屏。设备制造商也可以选择在搭载Android 8.1或更低版本的设备上支持刘海屏。

官方地址:

https://developer.android.com/guide/topics/display-cutout

Android 9.0及以上:

layoutInDisplayCutoutMode

Android允许控制是否在刘海区域内显示内容。窗口布局属性layoutInDisplayCutoutMode控制内容如何呈现在刘海区域中:

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT:这是默认行为,如上所述。在竖屏模式下,内容会呈现到刘海区域中;但在横屏模式下,内容会显示黑边。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES:在竖屏模式和横屏模式下,内容都会呈现到刘海区域中。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER:内容从不呈现到刘海区域中。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS:内容始终呈现到刘海区域中。
DisplayCutout
  • getBoundingRects():返回Rects的列表,每个Rects都是显示屏上非功能区域的边界矩形。
  • getSafeInsetLeft():返回安全区域距离屏幕左边的距离,单位是px。
  • getSafeInsetRight():返回安全区域距离屏幕右边的距离,单位是px。
  • getSafeInsetTop():返回安全区域距离屏幕顶部的距离,单位是px。
  • getSafeInsetBottom():返回安全区域距离屏幕底部的距离,单位是px。
通过代码演示不同窗口及layoutInDisplayCutoutMode的区别:
主题

将主题修改成不带ActionBar的样式


    
    

布局

将文本顶在布局的左上角,方便演示




    


代码

检测是否是刘海屏,如果存在刘海屏,则给布局增加一个PaddingTop

    @RequiresApi(api = Build.VERSION_CODES.P)
    private void detect() {
        final View decorView = getWindow().getDecorView();
        decorView.post(new Runnable() {
            @Override
            public void run() {
                WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
                if (rootWindowInsets == null) {
                    return;
                }
                DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
                if (displayCutout == null) {
                    return;
                }
                Log.e("yzt", "安全区域距离屏幕左边的距离>>>" + displayCutout.getSafeInsetLeft());
                Log.e("yzt", "安全区域距离屏幕右部的距离>>>" + displayCutout.getSafeInsetRight());
                Log.e("yzt", "安全区域距离屏幕顶部的距离>>>" + displayCutout.getSafeInsetTop());
                Log.e("yzt", "安全区域距离屏幕底部的距离>>>" + displayCutout.getSafeInsetBottom());
                List rects = displayCutout.getBoundingRects();
                if (rects == null || rects.size() == 0) {
                    Log.e("yzt", "不是刘海屏");
                } else {
                    Log.e("yzt", "刘海屏数量>>>" + rects.size());
                    for (Rect rect : rects) {
                        Log.e("yzt", "刘海屏区域>>>" + rect);
                    }
                    layout.setPadding(0, displayCutout.getSafeInsetTop(), 0, 0);
                }
            }
        });
    }
全屏情况

设置全屏,写在setContentView()之前

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

设置layoutInDisplayCutoutMode

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_XXXXX;
        getWindow().setAttributes(lp);

LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT模式


1.jpg

LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES模式


2.jpg

LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER模式
3.jpg

LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS模式


4.jpg
透明状态栏情况
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT模式


5.jpg

LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES模式


6.jpg

LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER模式
7.jpg

LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS模式


8.jpg
可以看出当刘海区域完全在系统的状态栏时,LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT的显示效果与LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES一致。
完整代码
public class MainActivity extends AppCompatActivity {

    private View layout;

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_main);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        //在竖屏模式下,内容会呈现到刘海区域中;但在横屏模式下,内容会显示黑边。
//        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
        //在竖屏模式和横屏模式下,内容都会呈现到刘海区域中。
        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        //内容从不呈现到刘海区域中。
//        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
        //内容始终呈现到刘海区域中。
//        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
        getWindow().setAttributes(lp);
        layout = findViewById(R.id.layout);
        detect();
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    private void detect() {
        final View decorView = getWindow().getDecorView();
        decorView.post(new Runnable() {
            @Override
            public void run() {
                WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
                if (rootWindowInsets == null) {
                    return;
                }
                DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
                if (displayCutout == null) {
                    return;
                }
                Log.e("yzt", "安全区域距离屏幕左边的距离>>>" + displayCutout.getSafeInsetLeft());
                Log.e("yzt", "安全区域距离屏幕右部的距离>>>" + displayCutout.getSafeInsetRight());
                Log.e("yzt", "安全区域距离屏幕顶部的距离>>>" + displayCutout.getSafeInsetTop());
                Log.e("yzt", "安全区域距离屏幕底部的距离>>>" + displayCutout.getSafeInsetBottom());
                List rects = displayCutout.getBoundingRects();
                if (rects == null || rects.size() == 0) {
                    Log.e("yzt", "不是刘海屏");
                } else {
                    Log.e("yzt", "刘海屏数量>>>" + rects.size());
                    for (Rect rect : rects) {
                        Log.e("yzt", "刘海屏区域>>>" + rect);
                    }
                    layout.setPadding(0, displayCutout.getSafeInsetTop(), 0, 0);
                }
            }
        });
    }

}

Android 9.0以下

上面是Android P才有的解决方案。然而国产厂商在Android P之前(基本都是Android O)就用上了高档大气上档次的刘海屏,所以,这也造就了各大厂商在Android P之前的解决方案百花齐放。这里不详细介绍。

结尾

核心思路就是判断是否带有刘海屏,根据自己的项目判断窗口的情况,再去决定是否需要做适配,如果需要适配再去根据实际的设计要求做适配。

你可能感兴趣的:(Android 9.0 适配——刘海屏)