Android全屏显示刘海屏适配

本文介绍了在Android中通过代码和主题设置实现全屏显示,同时适应刘海屏状态栏的技术,包括FLAG_FULLSCREEN、windowLayoutInDisplayCutoutMode等关键属性的使用。
摘要由CSDN通过智能技术生成

状态栏全屏适配

通过代码实现全屏:
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.fragment.app.FragmentActivity;

public class FullScreenUtil {
    /**
     * 全屏布局,不显示状态栏,适配刘海屏
     */
    public static void fullScreen(FragmentActivity activity) {
        Window window = activity.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        //允许window 的内容可以上移到刘海屏状态栏
        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);
        }
    }
}

通过设置主题theme实现全屏

默认values

<style name="PhotoTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:immersive">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

Android19, values-v19

<style name="PhotoTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:immersive">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

Android28, values-v28

<style name="PhotoTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:immersive">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

如果你看到了这里,觉得文章写得不错就给个赞呗?
更多Android进阶指南 可以扫码 解锁更多Android进阶资料


在这里插入图片描述
敲代码不易,关注一下吧。ღ( ´・ᴗ・` )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值