如何在安卓中创建一个无需包的启动画面

huangapple go评论82阅读模式
英文:

How to create a Splash screen without a package in android

问题

我在使用启动屏包时遇到了问题,有没有其他方法可以在安卓上设置启动屏,而不依赖于包。

参考链接:https://github.com/crazycodeboy/react-native-splash-screen

我尝试了仓库中列出的所有可能的方法,但仍然无法成功,可能是包已经过时了(版本升级)或者可能是我的一些人为错误。

英文:

I am facing problem while using the package of Splash Screen, Is there any other way I can set up a splash screen without the help of a package (in android)
reference: https://github.com/crazycodeboy/react-native-splash-screen

I tried all the possible methods listed in the repo, but still failed to do so either the package is too old now (version upgrade) or maybe some human error at my end.

答案1

得分: 0

你需要直接修改Android项目文件。以下是步骤:

  • 在你喜欢的代码编辑器中打开你的React Native项目。

  • 导航到你项目的android/app/src/main目录。

  • 如果不存在的话,创建一个名为drawable的新目录。这是你将存放启动屏幕图像的位置。

  • 将你的启动屏幕图像(通常是一个PNG文件)放入drawable目录中。确保使用正确分辨率的图像适配不同屏幕密度(mdpihdpixhdpixxhdpixxxhdpi)。

  • 现在,你需要修改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

打开位于android/app/src/main/res/values目录中的styles.xml文件。添加以下样式以定义你的启动屏幕主题:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@layout/launch_screen</item>
</style>

现在,打开位于android/app/src/main目录中的AndroidManifest.xml文件。

定位带有android:name=".MainActivity"属性的<activity>标签。

<activity>标签添加android:theme属性,指定你之前定义的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项目。

英文:

you'll need to modify the Android project files directly. Here are the steps.

  • Open your React Native project in your preferred code editor.

  • Navigate to the android/app/src/main directory of your project.

  • Create a new directory called drawable if it doesn't already exist.
    This is where you'll store your splash screen image.

  • Place your splash screen image (usually a PNG file) inside the
    drawable directory. Make sure to use an image with the correct
    resolution for different screen densities (mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi).

  • Now, you need to modify the launch_screen.xml file. If it doesn't
    exist, create it in the android/app/src/main/res/layout directory.
    This file will define the layout for your splash screen.

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;
        &lt;ImageView
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:src=&quot;@drawable/splash_screen_image&quot;
            android:scaleType=&quot;centerCrop&quot; /&gt;
    &lt;/RelativeLayout&gt;
    

Replace splash_screen_image with the actual name of your splash screen image file.

Open the styles.xml file located in the android/app/src/main/res/values directory. Add the following style to define the theme for your splash screen:

&lt;style name=&quot;SplashTheme&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
    &lt;item name=&quot;android:windowBackground&quot;&gt;@layout/launch_screen&lt;/item&gt;
&lt;/style&gt;

Now, open the AndroidManifest.xml file located in the android/app/src/main directory.

Locate the &lt;activity&gt; tag with the android:name=&quot;.MainActivity&quot; attribute.

Add the android:theme attribute to the &lt;activity&gt; tag, specifying the SplashTheme style you defined earlier:

&lt;activity
    android:name=&quot;.MainActivity&quot;
    android:label=&quot;@string/app_name&quot;
    android:theme=&quot;@style/SplashTheme&quot;&gt;

Save all the modified files and rebuild your Android project by running react-native run-android or ./gradlew clean &amp;&amp; react-native run-android in the project root directory.

答案2

得分: 0

好的。以下是翻译好的部分:

"good day. I highly recommend this npm package. I used it for a splash screen and it's really amazing, you just need to follow the steps and you are good to go.
https://www.npmjs.com/package/react-native-bootsplash"

英文:

good day. I highly recommend this npm package. I used it for a splash screen and it's really amazing, you just need to follow the steps and you are good to go.
https://www.npmjs.com/package/react-native-bootsplash

huangapple
  • 本文由 发表于 2023年5月28日 02:48:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定