Android适配刘海屏+沉浸式状态栏+启动页白屏解决方案

本文详细介绍了如何适配刘海屏设备,包括小米、OPPO、VIVO和华为等品牌,通过在AndroidManifest.xml中配置相应元数据实现。同时,提供了沉浸式状态栏的实现方法,通过创建SystemUI工具类并定义BaseActivity基类,实现状态栏的全屏显示。此外,还解决了启动首页出现白屏的问题,通过设置SplashActivity的主题样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、刘海屏适配可 参考对应手机官方文档

小米:dev.mi.com/console/doc/detail?pld=1160

oppo、vivo:open.oppomobile.com/wiki/doc#id=10159

在Manifest.xmlz中配置-application中插入

     <!-- OPPO -->
        <meta-data
            android:name="android.max_aspect"
            android:value="2.2" />
    <!-- 小米适配 -->
        <meta-data
            android:name="notch.config"
            android:value="portrait|landscape" /> 
    <!-- 允许绘制到华为刘海屏机型的刘海区域 -->
        <meta-data
            android:name="android.notch_support"
            android:value="true" />

二、沉浸式状态栏

新建工具类SystemUI.java,具体实现如下:

public class SystemUI {

    public static void fixSystemUI(Activity mActivity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //获取最顶层的View
            mActivity.getWindow().getDecorView()
                    .setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            mActivity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }
}

定义基类BaseActivity,在BaseActivity中的OnCreate()方法中调用:
        

//沉浸式状态栏实现 

SystemUI.fixSystemUI(this);

     后边用到的activity页面都可以继承该基类BaseActivity,即可实现沉浸式状态栏。

三、 启动首页出现白屏

导致原因

theme用的是Theme.AppCompat.Light.NoActionBar:

<application
        android:name=".application.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        tools:replace="icon, label, theme">

解决办法

添加一个新的style:

<activity
            android:name=".activity.SplashActivity"
            android:screenOrientation="portrait"
            android:theme="@style/styleSplash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

style中windowIsTranslucent属性设置为true,背景透明:

<style name="styleSplash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
    </style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值