Android 刘海屏全屏适配(沉溺式状态栏,隐藏状态栏)

Android 刘海屏全屏适配(沉溺式状态栏,隐藏状态栏)_第1张图片

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE) //这行代码一定要在setContentView之前,不然会闪退
        setContentView(R.layout.activity_welcome)
        val window: Window = window
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
//        把布局画到刘海屏
        NotchScreenManager.getInstance().setDisplayInNotch(this)
        init()
    }

NotchScreenTool 适配刘海屏水滴屏等全面屏工具
具体的适配过程可以查看这篇博客:

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

安装
项目根目录的build.gradle中添加:

allprojects {
repositories {
google()
jcenter()
maven { url “https://jitpack.io” } // 添加jitpack
}
}
在module层级的build.gradle中添加依赖

implementation ‘com.github.smarxpan:NotchScreenTool:0.0.1’
使用
设置UI显示到刘海区域

NotchScreenManager.getInstance().setDisplayInNotch(Activity activity);
默认情况下,应用的界面不会绘制到刘海区域,调用此方法可允许绘制。

获取刘海区域信息

NotchScreenManager.getInstance().getNotchInfo(activity, 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());
}
}
}
});

https://github.com/smarxpan/NotchScreenTool

你可能感兴趣的:(Android)