Android P (9.0)刘海屏(DisplayCutout)适配方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/f2006116/article/details/89006679

简介

Android P版本提供了统一的刘海屏方案和三方适配刘海屏方案:

  • 对于有状态栏的页面,不会受到刘海屏特性的影响

  • 全屏显示的页面,系统刘海屏方案会对应用界面做下移处理,避开刘海区显示

  • 已经适配Android P应用的全屏页面可以通过谷歌提供的适配方案使用刘海区,真正做到全屏显示。

Android P 为最新的刘海屏,提供了专门的Api来支持:DisplayCutout。

Google 提供的适配方案,可以设置是否在全屏模式下,使用刘海屏的区域。

新的布局属性 layoutInDisplayCutoutMode 包含三种可选的模式,分别为:

// 窗口声明使用刘海区域
public static final int LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES = 1;
// 默认情况下,全屏窗口不会使用到刘海区域,非全屏窗口可正常使用刘海区域
public static final int LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT = 0;
// 声明不使用刘海区域
public static final int LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER = 2;

开发者选项Debug模拟

默认的Android P的模拟器是关闭刘海屏的,首先看下如何开启Android P的模拟器中的刘海屏,

1)启用开发者选项和调试 
-> 打开 Settings 应用。 
-> 选择 System。 
-> 滚动到底部,然后选择 About phone。 
-> 滚动到底部,点按 Build number 7 次。 
-> 返回上一屏幕,在底部附近可找到 Developer options

2)在 Developer options 屏幕中,向下滚动至 Drawing 部分并选择 Simulate a display with a cutout

3)选择屏幕缺口的大小

                                            

如图所示,Google提供了四种刘海屏选择方案 
 
对应上面4中选择 
第一种,很显然没有刘海屏 
第二种到第四种,只不过是刘海屏宽度不一样罢了,我这里选择的是Tall display cutout模式,如下图所示 

                                     
这里需要注意一个问题:使用Android P模拟器的时候,这里的模拟凹口屏的四个选项其实实质只改变了刘海屏的位置和宽度,高度(系统刘海屏不会超过状态栏),但是在真机中,这四个选项有可能对应不同的操作。

刘海屏样式

                                    

                                    

                                     

                                    

适配方案(全屏方案,以Activity为例)

1.

++import android.view.Window;

++import android.view.WindowManager;

2.

++WindowManager.LayoutParams lp = this.getWindow().getAttributes();

++lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
++this.getWindow().setAttributes(lp);

谷歌文档连接

https://source.android.google.cn/devices/tech/display/display-cutouts?hl=zh-tw

https://source.android.com/devices/tech/display/display-cutouts

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

https://android-developers.googleblog.com/2017/12/tuning-your-apps-and-games-for-long.html

 

参考资料

https://www.jianshu.com/p/561f7241153b/

https://blog.csdn.net/H176Nhx7/article/details/80417049

https://blog.csdn.net/yi_master/article/details/80309757

猜你喜欢

转载自blog.csdn.net/f2006116/article/details/89006679