React Native安卓刘海屏适配终极方案:仅需修改 AndroidManifest.xml!

📌 问题背景

在 React Native 开发中,我们经常会遇到安卓设备刘海屏(Notch)适配问题。即使正确使用了 react-native-safe-area-contextreact-navigation,在一些安卓设备(如小米、华为、OPPO 等)上,头部内容仍然可能被刘海遮挡。

经过反复测试,我发现:**只需在 AndroidManifest.xml 中添加几行配置,即可完美解决!**无需复杂代码改动!


✅ 解决方案

✨ 只需一步:修改 AndroidManifest.xml

找到项目路径下的:

android/app/src/main/AndroidManifest.xml

<application> 标签中添加以下配置:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.your.app">

    <application
        android:name=".MainApplication"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:allowBackup="true"
        android:theme="@style/AppTheme">

        <!-- 启用安卓官方刘海屏支持 -->
        <meta-data
            android:name="android.notch_support"
            android:value="true" />

        <!-- 针对小米MIUI系统的刘海屏适配 -->
        <meta-data
            android:name="notch.config"
            android:value="portrait|landscape" />

        <!-- 针对华为EMUI系统的刘海屏适配 -->
        <meta-data
            android:name="android.max_aspect"
            android:value="2.3" />

         <!-- 针对OPPO系统的刘海屏适配 -->
        <meta-data 
            android:name="com.oppo.feature.screen.heteromorphism" 
            android:value="true" />

        <!-- 其他已有配置继续保留 -->
        
    </application>
</manifest>

在华为鸿蒙系统,需要在MainActivity.kt(MainActivity.java)里面添加遗下代码

package com.fujfu.fyh

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
// import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import org.devio.rn.splashscreen.SplashScreen

class MainActivity : ReactActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    
    // 设置状态栏透明 解决刘海屏问题 鸿蒙系统
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
      window.decorView.systemUiVisibility = 
        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
      window.statusBarColor = android.graphics.Color.TRANSPARENT
    }
    
    SplashScreen.show(this)
  }

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  override fun getMainComponentName(): String {
    return "fyhMobile"
  }

  /**
   * Returns the instance of the {@link ReactActivityDelegate}. Here we use a util class {@link
   * DefaultReactActivityDelegate} which allows you to easily enable Fabric and Concurrent React
   * (aka React 18) with two boolean flags.
   */
  override fun createReactActivityDelegate(): ReactActivityDelegate =
    DefaultReactActivityDelegate(
      this,
      mainComponentName,
      fabricEnabled // fabricEnabled
    )
}


📚 配置项说明

配置项说明适用设备
android.notch_support启用安卓官方刘海屏支持所有安卓设备
notch.config适配小米 MIUI 系统的刘海屏小米
android.max_aspect设置最大宽高比,防止内容被刘海遮挡华为

❓ 为什么这个方法有效?

  1. android.notch_support
    告诉系统你的应用支持刘海屏,系统会自动为你处理状态栏和内容区域的安全布局。

  2. notch.config(小米专属)
    小米系统自定义了刘海屏配置,该配置确保你的 App 在竖屏和横屏下都能正确适配刘海区域。

  3. android.max_aspect(华为专属)
    华为 EMUI 系统下,如果不设置此项,App 可能会默认采用缩放模式显示,导致顶部内容被遮挡。


⚠️ 注意事项

  • 无需修改 JS 代码:适配全部在原生层完成,对 React Native 的 JavaScript 层无侵入。

  • 无需额外库:不需要安装 react-native-device-inforeact-native-page-wrapper 等额外依赖。

  • 已验证兼容性:实测小米、华为、OPPO 等多种设备,均适配良好。

  • 🔁 修改后请重新编译 APK

npx react-native run-android

🔚 总结

通过仅修改 AndroidManifest.xml,即可彻底解决 React Native 在安卓刘海屏设备上的适配问题:

  • ✅ 不写一行 JS 代码

  • ✅ 不引入任何额外库

  • ✅ 适配主流国产机型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wen's

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值