react native开发Android 篇——APP名称、图标、启动页

react native开发Android 篇——APP名称、图标、启动页

设置APP名称

编辑 android/app/src/main/res/values/strings.xml 文件:

<resources>
    <string name="app_name">倒数日</string>
</resources>

设置APP图标

使用图标工场处理图片,拿到各个尺寸的图标,替换 android/app/src/main/res 下对应的图标。

图标工场处理过的图标:
图标工场处理结果

要替换的图标:
要替换的图标

设置启动页

添加启动页可以使用 react-native-splash-screen 库,通过它可以控制启动页的显示和隐藏。
安装

yarn add react-native-splash-screen/npm install react-native-splash-screen --save
react-native link react-native-splash-screen

编辑 android/app/src/main/java/com/daysmatter(自己的项目名称)/MainActivity.java,添加显示启动页的代码:

// 启动页设置添加代码
import android.os.Bundle;
import org.devio.rn.splashscreen.SplashScreen;
public class MainActivity extends ReactActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 这里定义了在加载js的时候,同时弹起启动屏
        // 第二个参数true,是启动页全屏显示,隐藏了状态栏。
        SplashScreen.show(this, true);
    }
    ...其他代码
}

android/app/src/main/res/layout 文件夹下创建启动页布局文件 launch_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_test"
    android:gravity="center">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="倒数日"
            android:textSize="60sp"
            android:textStyle="bold"
            android:textColor="@color/StartTitle"
            
        />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住每个重要时刻"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginTop="30dp"
            android:textColor="@color/StartSubtitle"
        />
</LinearLayout>

绘制背景色渐变:新建android/app/src/main/res/drawable/gradient_test.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="270"
        android:endColor="#00A6FE"
        android:centerColor="#eeeeee"
        android:startColor="#02D7FF" />
</shape>

解决短暂白屏可以将背景设置为透明,编辑android/app/src/main/res/values/styles.xml即可。

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <!--设置透明背景-->
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

隐藏启动页

编辑App.js文件,在componentDidMount中关闭启动页

componentDidMount() {
  // 组件加载完毕之后,隐藏启动页
    this.timer = setTimeout(() => {
      SplashScreen.hide();
    }, 900)
}
//卸载计时器
componentWillUnmount() {
  this.timer && clearTimeout(this.timer);//同时为真的才执行卸载
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值