我将适配方案整理后,封装成了一个库并上传至github,可参考使用html
项目地址: github.com/smarxpan/No…android
市面上的屏幕尺寸和全面屏方案五花八门。git
这里我使用了小米的图来讲明:github
上述两种屏幕均可以统称为刘海屏,不过对于右侧较小的刘海,业界通常称为水滴屏或美人尖。为便于说明,后文提到的「刘海屏」「刘海区」都同时指代上图两种屏幕。c#
其中第一点是全部应用都须要适配的,对应下文的声明最大长宽比
api
而第二点,若是应用自己不须要全屏显示或使用沉浸式状态栏,是不须要适配的。app
针对须要适配第二点的应用,须要获取刘海的位置和宽高,而后将显示内容避开便可。ide
之前的普通屏长宽比为16:9,全面屏手机的屏幕长宽比增大了不少,若是不适配的话就会相似下面这样:函数
黑色区域为未利用的区域。布局
适配方式有两种:
将targetSdkVersion版本设置到API 24及以上
这个操做将会为<application>
标签隐式添加一个属性,android:resizeableActivity="true"
, 该属性的做用后面将详细说明。
在 <application>
标签中增长属性:android:resizeableActivity="false"
同时在节点下增长一个meta-data
标签:
<!-- 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:resizeableActivity属性。
在 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"
。 不然最大长宽比没有任何做用。
Android P(9.0)开始,官方提供了适配异形屏的方式。
经过全新的 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 的设备或模拟器上模拟屏幕缺口:
适配参考:
// 延伸显示区域到刘海
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>
复制代码
因Google官方的适配方案到Android P才推出,所以在Android O设备上,各家厂商有本身的实现方案。
我这里主要适配了华为、小米、oppo,这三家都给了完整的解决方案。至于vivo,vivo给了判断是否刘海屏的API,可是没用设置刘海区域显示到API,所以无需适配。
方案一:
具体方式以下所示:
<meta-data android:name="android.notch_support" android:value="true"/>
复制代码
对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>
复制代码
对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生效,意味着该应用的全部页面,系统都不会作竖屏场景的特殊下移或者是横屏场景的右移特殊处理
个人NotchScreenTool中使用的就是方案二,若是须要针对Activity,建议自行修改。
设置应用窗口在华为刘海屏手机使用刘海区
/*刘海屏全屏显示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");
}
}
复制代码
清除添加的华为刘海屏Flag,恢复应用不使用刘海区显示
/**
* 设置应用窗口在华为刘海屏手机使用刘海区
* @param window 应用页面window对象
*/
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");
}
}
复制代码
判断是不是刘海屏
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;
}
复制代码
设置显示到刘海区域
@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) {
}
}
复制代码
获取刘海宽高
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;
}
复制代码
判断是不是刘海屏
@Override
public boolean hasNotch(Activity activity) {
boolean ret = false;
try {
ret = activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
} catch (Throwable ignore) {
}
return ret;
}
复制代码
获取刘海的左上角和右下角的坐标
/**
* 获取刘海的坐标
* <p>
* 属性形如:[ro.oppo.screen.heteromorphism]: [378,0:702,80]
* <p>
* 获取到的值为378,0:702,80
* <p>
* <p>
* (378,0)是刘海区域左上角的坐标
* <p>
* (702,80)是刘海区域右下角的坐标
*/
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;
}
复制代码
Oppo Android O机型不须要设置显示到刘海区域,只要设置了应用全屏就会默认显示。
所以Oppo机型必须适配。
根据上述功能,我将其整理成了一个依赖库:NotchScreenTool
使用起来很简单:
// 支持显示到刘海区域
NotchScreenManager.getInstance().setDisplayInNotch(this);
// 获取刘海屏信息
NotchScreenManager.getInstance().getNotchInfo(this, new INotchScreen.NotchScreenCallback() {
@Override
public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
if (notchScreenInfo.hasNotch) {
for (Rect rect : notchScreenInfo.notchRects) {
Log.i(TAG, "notch screen Rect = " + rect.toShortString());
}
}
}
});
复制代码
获取刘海区域信息后就能够根据本身应用的须要,来避开重要的控件。
详情可参考我项目中的代码。