现在,市面上的屏幕尺寸和全面屏方案五花八门。这里我使用了小米的图来说明:上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖。为便于说明,后文提到的「刘海屏」「刘海区」都同时指代上图两种屏幕。

刘海屏、水滴屏全面屏适配细节

当我们在谈屏幕适配时,我们具体谈什么呢?

  • 适应更长的屏幕

  • 防止内容被刘海遮挡

其中第一点是所有应用都需要适配的,对应下文的声明最大长宽比,而第二点,如果应用本身不需要全屏显示或使用沉浸式状态栏,是不需要适配的。

针对需要适配第二点的应用,需要获取刘海的位置和宽高,然后将显示内容避开即可。

声明最大长宽比

以前的普通屏长宽比为16:9,全面屏手机的屏幕长宽比增大了很多,如果不适配的话就会类似下面这样:

适配方式

适配方式有两种:

  1. 将targetSdkVersion版本设置到API 24及以上;

这个操作将会为<application> 标签隐式添加一个属性,android:resizeableActivity="true", 该属性的作用后面将详细说明。

  1. 在 <application> 标签中增加属性:android:resizeableActivity="false",同时在节点下增加一个meta->

 <!-- Render on full screen up to screen aspect ratio of 2.4 --> <!-- Use a letterbox on screens larger than 2.4 --> <meta-data android:name="android.max_aspect" android:value="2.4" />

原理说明

在 Android 7.0(API 级别 24)或更高版本的应用,android:resizeableActivity属性默认为true(对应适配方式1)。这个属性是控制多窗口显示的,决定当前的应用或者Activity是否支持多窗口。

可以在清单的<activity>或 <application>节点中设置该属性,启用或禁用多窗口显示,配置如下:

android:resizeableActivity=["true" | "false"]

如果该属性设置为 true,Activity 将能以分屏和自由形状模式启动。 如果此属性设置为 false,Activity 将不支持多窗口模式。 如果该值为 false,且用户尝试在多窗口模式下启动 Activity,该 Activity 将全屏显示。

适配方式2即为设置屏幕的最大长宽比,这是官方提供的设置方式。如果设置了最大长宽比,必须android:resizeableActivity="false"。 否则最大长宽比没有任何作用。

适配刘海屏

Android9.0适配

Android P(9.0)开始,官方开始提供了官方的挖孔屏适配API,具体可以参考Support display cutouts。通过Android P提供的 DisplayCutout 类,可以确定非功能区域的位置和形状,这些区域不应显示内容。 要确定这些凹口屏幕区域是否存在及其位置,请使用 getDisplayCutout() 函数。

全新的窗口布局属性 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_DEFAULT,刘海区域不会显示内容,需要显示时可以将值设置为LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES。

您可以按如下步骤在任何运行 Android P 的设备或模拟器上模拟屏幕缺口:

  1. 启用开发者选项;

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

适配参考示例:

// 延伸显示区域到刘海 WindowManager.LayoutParams lp = window.getAttributes(); lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; window.setAttributes(lp); // 设置页面全屏显示 final View decorView = window.getDecorView(); decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

其中延伸显示区域到刘海的代码,也可以通过修改Activity或应用的style实现,例如:

 <?xml version="1.0" encoding="utf-8"?> <resources>     <style name="AppTheme" parent="xxx">         <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>     </style> </resources>

Android O 适配

因Google官方的适配方案到Android P才推出,因此在Android O(8.0版本)设备上,各家厂商有自己的实现方案。

华为Android O适配

方案一:

  1. 具体方式如下所示:

<meta-data android:name="android.notch_support" android:value="true"/>
  1. 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理。例如:

<application     android:allowBackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:roundIcon="@mipmap/ic_launcher_round"     android:testOnly="false"     android:supportsRtl="true"     android:theme="@style/AppTheme">     <meta-data android:name="android.notch_support" android:value="true"/>     <activity android:name=".MainActivity">         <intent-filter>             <action android:name="android.intent.action.MAIN"/>             <category android:name="android.intent.category.LAUNCHER"/>         </intent-filter> </activity>
  1. 对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理。例如:

<application     android:allowBackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:roundIcon="@mipmap/ic_launcher_round"     android:testOnly="false"     android:supportsRtl="true"     android:theme="@style/AppTheme">     <activity android:name=".MainActivity">         <intent-filter>             <action android:name="android.intent.action.MAIN"/>              <category android:name="android.intent.category.LAUNCHER"/>         </intent-filter>     </activity>     <activity android:name=".LandscapeFullScreenActivity" android:screenOrientation="sensor">     </activity>     <activity android:name=".FullScreenActivity">         <meta-data android:name="android.notch_support" android:value="true"/>     </activity> </application>

方案二:对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理。

1,设置应用窗口在华为刘海屏手机使用刘海区。

/*刘海屏全屏显示FLAG*/ public static final int FLAG_NOTCH_SUPPORT=0x00010000; /**  * 设置应用窗口在华为刘海屏手机使用刘海区  * @param window 应用页面window对象  */ public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {     if (window == null) {         return;     }     WindowManager.LayoutParams layoutParams = window.getAttributes();     try {         Class layoutParamsExCls = Class.forName("com.huawei.android.view.LayoutParamsEx");         Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);         Object layoutParamsExObj=con.newInstance(layoutParams);         Method method=layoutParamsExCls.getMethod("addHwFlags", int.class);         method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);     } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException      | InvocationTargetException e) {         Log.e("test", "hw add notch screen flag api error");     } catch (Exception e) {         Log.e("test", "other Exception");     } }

2.清除添加的华为刘海屏Flag,恢复应用不使用刘海区显示。

 public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) {     if (window == null) {         return;     }     WindowManager.LayoutParams layoutParams = window.getAttributes();     try {         Class layoutParamsExCls = Class.forName("com.huawei.android.view.LayoutParamsEx");         Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);         Object layoutParamsExObj=con.newInstance(layoutParams);         Method method=layoutParamsExCls.getMethod("clearHwFlags", int.class);         method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);     } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException      | InvocationTargetException e) {         Log.e("test", "hw clear notch screen flag api error");     } catch (Exception e) {         Log.e("test", "other Exception");     } }

小米Android O适配

  1. 判断是否是刘海屏。

 private static boolean isNotch() {     try {         Method getInt = Class.forName("android.os.SystemProperties").getMethod("getInt", String.class, int.class);         int notch = (int) getInt.invoke(null, "ro.miui.notch", 0);         return notch == 1;     } catch (Throwable ignore) {     }     return false; }
  1. 设置显示到刘海区域

@Override public void setDisplayInNotch(Activity activity) {     int flag = 0x00000100 | 0x00000200 | 0x00000400;     try {         Method method = Window.class.getMethod("addExtraFlags",                 int.class);         method.invoke(activity.getWindow(), flag);     } catch (Exception ignore) {     } }
  1. 获取刘海宽高

public static int getNotchHeight(Context context) {     int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");     if (resourceId > 0) {         return context.getResources().getDimensionPixelSize(resourceId);     }     return 0; } public static int getNotchWidth(Context context) {     int resourceId = context.getResources().getIdentifier("notch_width", "dimen", "android");     if (resourceId > 0) {         return context.getResources().getDimensionPixelSize(resourceId);     }     return 0; }

oppo Android O适配

  1. 判断是否是刘海屏

@Override public boolean hasNotch(Activity activity) {     boolean ret = false;     try {         ret = activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");     } catch (Throwable ignore) {     }     return ret; }
  1. 获取刘海的左上角和右下角的坐标

private static String getScreenValue() {     String value = "";     Class<?> cls;     try {         cls = Class.forName("android.os.SystemProperties");         Method get = cls.getMethod("get", String.class);         Object object = cls.newInstance();         value = (String) get.invoke(object, "ro.oppo.screen.heteromorphism");     } catch (Throwable ignore) {     }     return value; }

品略图书馆 http://www.pinlue.com/

转载于:https://blog.51cto.com/14325182/2406502

Android刘海屏、水滴屏全面屏适配。相关推荐

  1. 【Android】【手机适配】Android自定义导航栏和全面屏适配方案

    名词说明 状态栏:StatusBar,手机上方显示电量.时间的横条 导航栏:NavigationBar,手机下方显示虚拟按键的横条 标题栏:ActionBar,应用顶部显示标题的横条 全面屏:界面内容 ...

  2. Android 闪屏启动页全面屏适配

    随着全面屏手机普及,目前市面上的手机屏幕尺寸大致分为三种: 非全面屏(16:9),全面屏(18:9),全面屏(>18:9) App启动初始化程序会出现短暂的白屏问题,为解决白屏可设置启动页面样式 ...

  3. android车载支持格式,安卓全面屏适配攻略(适配超长车载主机)

    前言 2017年9月,拜腾的横空出世,打破了车载主机界一直以来的沉寂,各大媒体也是不吝词藻的对它的超长中控屏进行了大肆的报道.这个时候作为同为车机供应者的诸位友商心里却不那么的平静,恨不得在发布会现场 ...

  4. Android刘海屏、水滴屏全面屏适配方案

    原文地址:https://www.jianshu.com/p/2b8db60ba8df 我将适配方案整理后,封装成了一个库并上传至github,可参考使用 项目地址: https://github.c ...

  5. Android刘海屏、水滴屏全面屏适配

    现在,市面上的屏幕尺寸和全面屏方案五花八门.这里我使用了小米的图来说明: 上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖.为便于说明,后文提到的「刘海屏」「刘海区」 ...

  6. Android刘海屏、水滴屏全面屏适配方案,海量算法高频面试题精编解析

    对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理: 方案二 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的 ...

  7. Android刘海屏、水滴屏全面屏适配详解

    现在,市面上的屏幕尺寸和全面屏方案五花八门.这里我使用了小米的图来说明: 上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖.为便于说明,后文提到的「刘海屏」「刘海区」 ...

  8. Java安卓适配全面屏_GitHub - chnagzhaolin/NotchScreenTool: Android刘海屏、水滴屏等全面屏适配工具...

    NotchScreenTool 适配刘海屏水滴屏等全面屏工具 具体的适配过程可以查看这篇博客: 安装 项目根目录的build.gradle中添加: allprojects { repositories ...

  9. 刘海屏水滴屏小米89等安卓P底部留黑or白适配

    刘海屏水滴屏小米89等安卓P底部留黑or白适配 一 问题 一些老旧项目安装到目前安卓高版本,会出现手机底部留黑or白 二 瞬秒解决 Android官方提供了适配方案,即提高App所支持的最大屏幕纵横比 ...

最新文章

  1. 力扣(LeetCode)刷题,简单+中等题(第29期)
  2. Fedora 7 播放器totem
  3. CICC科普栏目丨时间之箭:从熵到大爆炸再到万物理论(一)
  4. 10个 Python 程序员,9个不合格?
  5. linux内存分配器类型,内核早期内存分配器:memblock
  6. [国嵌攻略][038][时钟初始化]
  7. ajax 最大链接数_[LeetCode] 479. 最大回文数乘积
  8. Spring设计模式之装饰器模式
  9. Ubuntu开机加速
  10. java精选视频资源,收藏慢慢看!
  11. 华为p20nfc怎么复制门禁卡_华为荣耀手机的NFC功能怎么用?怎么刷门禁卡
  12. 好用的 Puppeteer 辅助工具 Puppeteer Recorder
  13. ulimit命令参数及用法
  14. 新刷的小米手机系统没有便签,刷系统清除数据怎么恢复
  15. 在Linux上解压缩和打包WAR文件
  16. nasm ces纠正性训练 nsca-cpt体能训练 pes cscs 体能训练 acsm
  17. 七个实用的分布式开源框架
  18. 已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“:
  19. KVM虚拟化之(1):CPU技术
  20. 最新 PhpStorm 2018 安装及破解方法

热门文章

  1. java replace替换空格_替换空格-java
  2. 文:你可以杀我,但你不能评价(judge)我
  3. icom对讲机写频线定义_Alinco/ICOM对讲机USB写频线
  4. 排序算法 Sorting Algorithm(一)
  5. Excel如何简单快速在行上输入ABC序列?
  6. SEO网站优化快速提升排名的方法
  7. Linux 常用的压缩方式
  8. 利用shell脚本打印图形
  9. 五分钟告诉你什么是MySQL的覆盖索引
  10. 经典面试题 之 SQL优化