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版本才可使用。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android 12 中,由于安全性的考虑,应用程序访问本地文件系统的方式发生了变化。从 Android 12 开始,应用程序将无法访问 `file:///android_asset/` 目录。相反,您应该使用 `content://` URI 或将文件复制到应用程序专用目录中的 `files/` 目录。 以下是一些适用于 Android 12 的解决方案: 1. 使用 `AssetManager` 类来获取 `assets` 目录中的文件。例如,您可以使用以下代码从 `assets` 目录中读取文本文件: ``` try { InputStream inputStream = getAssets().open("filename.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bufferedReader.readLine()) != null) { // process the line } bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } ``` 2. 将文件复制到应用程序专用目录中的 `files/` 目录,并使用 `FileProvider` 类来生成 `content://` URI。例如,您可以使用以下代码将文件复制到 `files/` 目录: ``` try { InputStream inputStream = getAssets().open("filename.txt"); FileOutputStream outputStream = openFileOutput("filename.txt", Context.MODE_PRIVATE); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } ``` 然后,您可以使用以下代码生成 `content://` URI: ``` File file = new File(getFilesDir(), "filename.txt"); Uri contentUri = FileProvider.getUriForFile(this, "your.package.name.fileprovider", file); ``` 在此示例中,`your.package.name` 应该替换为您的应用程序的包名。 请注意,如果您的应用程序需要访问其他应用程序的文件,则需要使用 `ACTION_OPEN_DOCUMENT` 或 `ACTION_CREATE_DOCUMENT` 操作来请求用户授权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值