现在, 市面上的屏幕尺寸和全面屏方案五花八门. 这里我使用了小米的图来说明:
上述两种屏幕都可以统称为刘海屏, 不过对于右侧较小的刘海, 业界一般称为水滴屏或美人尖. 为便于说明, 后文提到的「刘海屏」「刘海区」都同时指代上图两种屏幕.
刘海屏, 水滴屏全面屏适配细节
当我们在谈屏幕适配时, 我们具体谈什么呢?
适应更长的屏幕
防止内容被刘海遮挡
其中第一点是所有应用都需要适配的, 对应下文的声明最大长宽比, 而第二点, 如果应用本身不需要全屏显示或使用沉浸式状态栏, 是不需要适配的.
针对需要适配第二点的应用, 需要获取刘海的位置和宽高, 然后将显示内容避开即可.
声明最大长宽比
以前的普通屏长宽比为 16:9, 全面屏手机的屏幕长宽比增大了很多, 如果不适配的话就会类似下面这样:
适配方式
适配方式有两种:
将 targetSdkVersion 版本设置到 API 24 及以上;
这个操作将会为 < application> 标签隐式添加一个属性, Android:resizeableActivity="true", 该属性的作用后面将详细说明.
- <!-- 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" />
- LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
- LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
- LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
- // 延伸显示区域到刘海
- WindowManager.LayoutParams lp = Windows.getAttributes();
- lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
- Windows.setAttributes(lp);
- // 设置页面全屏显示
- final View decorView = Windows.getDecorView();
- decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="AppTheme" parent="xxx">
- <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
- </style>
- </resources>
- <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>
- <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>
- /* 刘海屏全屏显示 FLAG*/
- public static final int FLAG_NOTCH_SUPPORT=0x00010000;
- /**
- * 设置应用窗口在华为刘海屏手机使用刘海区
- * @param Windows 应用页面 Windows 对象
- */
- public static void setFullScreenWindowLayoutInDisplayCutout(Windows Windows) {
- if (Windows == null) {
- return;
- }
- WindowManager.LayoutParams layoutParams = Windows.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");
- }
- }
- public static void setNotFullScreenWindowLayoutInDisplayCutout (Windows Windows) {
- if (Windows == null) {
- return;
- }
- WindowManager.LayoutParams layoutParams = Windows.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 = Windows.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;
- }
- 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.jianshu.com/p/89b2eb253838