Android适配全面屏/刘海屏

目前国内厂商已经推出的刘海屏Android手机有华为P20 pro, vivo X21,OPPO R15。

1.华为刘海屏的官方适配文档 

https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114

2.oppo刘海屏官方文档:

https://open.oppomobile.com/service/message/detail?id=61876


3.vivo刘海屏官方文档

https://dev.vivo.com.cn/doc/document/info?id=103


vivo 和 OPPO官网仅仅给出了适配指导,没有给出具体方案,简单总结为:

如有是具有刘海屏的手机,竖屏显示状态栏,横屏不要在危险区显示重要信息或者设置点击事件

4.google官方刘海屏适配方案

google从Android P开始为刘海屏提供支持,目前提供了一个类和三种模式:

一个类

The new DisplayCutout class lets you find out the location and shape of the non-functional areas where content shouldn't be displayed. To determine the existence and placement of these cutout areas, use thegetDisplayCutout() method

就是说可以用DisplayCutout这个类找出刘海(cutout)的位置和形状,调用getDisplayCutout()这个方法可以获取刘海(cutout)的位置和区域

所以我们可用这个类判断是否有刘海的存在以及刘海的位置

DisplayCutout cutout = mContext.getDisplayCutout();

三种模式

A new window layout attribute, layoutInDisplayCutoutMode, allows your app to lay out its content around a device's cutouts. You can set this attribute to one of the following values:

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER

layoutInDisplayCutoutMode值说明:

LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT:默认情况下,全屏窗口不会使用到刘海区域,非全屏窗口可正常使用刘海区域

LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS:窗口声明使用刘海区域

LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER:窗口声明不使用刘海区域

适配方案:
我们可以设置是否允许window扩展到刘海区:

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

例如一个有状态栏的页面, 我们可以这样适配:
DisplayCutout cutout = getDisplayCutout();
if(cutout != null){
 WindowManager.LayoutParams lp =getWindow().getAttributes();  
 lp.layoutInDisplayCutoutMode=WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;  
 getWindow().setAttributes(lp);
}


需要注意的是:谷歌提供的刘海屏适配方案,要求应用必须适配到P版本才可使用。

你可能感兴趣的:(android开发)