我在使用启动画面包时遇到问题,有没有其他方法可以在没有包的帮助下设置启动画面(在android中) 参考:https://github.com/crazycodeboy/react-native-splash-screen
我尝试了存储库中列出的所有可能的方法,但仍然失败,要么包现在太旧(版本升级),要么可能是我的一些人为错误。
美好的一天。我强烈推荐这个 npm 包。我用它作为启动画面,它真的很棒,你只需要按照步骤操作就可以了。 https://www.npmjs.com/package/react-native-bootsplash
您需要直接修改Android项目文件。步骤如下。
在您首选的代码编辑器中打开您的 React Native 项目。
导航到项目的
android/app/src/main
目录。
创建一个名为drawable的新目录(如果它尚不存在)。 这是您存储启动屏幕图像的位置。
将闪屏图像(通常是 PNG 文件)放入 可绘制目录。确保使用正确的图像 不同屏幕密度的分辨率(
mdpi
、hdpi
、xhdpi
、xxhdpi
、xxxhdpi
)。
现在,您需要修改
launch_screen.xml
文件。如果没有的话
存在,请在 android/app/src/main/res/layout
目录中创建它。
该文件将定义启动屏幕的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash_screen_image"
android:scaleType="centerCrop" />
</RelativeLayout>
将
splash_screen_image
替换为初始屏幕图像文件的实际名称。
打开位于
styles.xml
目录中的 android/app/src/main/res/values
文件。添加以下样式来定义启动屏幕的主题:
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@layout/launch_screen</item>
</style>
现在,打开位于
AndroidManifest.xml
目录中的 android/app/src/main
文件。
找到具有
<activity>
属性的 android:name=".MainActivity"
标签。
将
android:theme
属性添加到 <activity>
标签,指定之前定义的 SplashTheme
样式:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/SplashTheme">
保存所有修改的文件并通过在项目根目录中运行
react-native run-android
或./gradlew clean && react-native run-android
来重建您的Android项目。
最近我发现了一个视频教程。并且运行良好。 https://www.youtube.com/watch?v=wOaWKcF_TdE