1、刘海机型说明
众所周知, 市面上的android 机型,屏幕尺寸五花八门,如下图所示:
像以上两种屏幕,我们都统称为【刘海屏】。对于右侧较小的刘海,业界一般称为【水滴屏】或【美人尖】。
2、刘海机型适配方案
从Android P(9.0)开始,官方提供了适配异形屏的方式。
通过全新的 DisplayCutout 类,可以确定非功能区域的位置和形状,这些区域不应显示内容。 要确定这些凹口屏幕区域是否存在及其位置,请使用 getDisplayCutout() 函数。
Android 允许控制是否在刘海区域内显示内容。
窗口布局属性 layoutInDisplayCutoutMode 控制内容如何呈现在刘海区域中。
可以将 layoutInDisplayCutoutMode
设为以下某个值:
- LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT 默认,在竖屏模式下,内容会呈现到刘海区域中;但在横屏模式下,内容会显示黑边。
- LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES- 在竖屏模式和横屏模式下,内容都会呈现到刘海区域中。
- LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER- 内容从不呈现到刘海区域中。
3、刘海机型适配参考
(1)、代码适配实现方式
// 延伸显示区域到刘海
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);
(2)、style实现方式
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="xxx">
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
</resources>
(3)、Dialog 下 完整全屏适配【刘海屏】示例:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
@SuppressLint("InflateParams")
public class TestatDialog extends Dialog{
public TestatDialog(Activity context, int dialogFullscreen) {
super(context, dialogFullscreen);
this.activity = context;
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
//全屏适配刘海机型
Window window = getWindow();
if (window !=null) {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.getDecorView().setPadding(0, 0, 0, 0);
window.getDecorView().setBackgroundColor(Color.WHITE);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.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);
}
window.setAttributes(layoutParams);
}
setContentView(LayoutInflater.from(this.activity).inflate(R.layout.lanuch_layout_view, null));
}
public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
if ((paramInt == KeyEvent.KEYCODE_BACK) && (paramKeyEvent.getRepeatCount() == 0)) {
return true;
}
return super.onKeyDown(paramInt, paramKeyEvent);
}
@Override
public void dismiss() {
}
}