首页 > 系统 > Android > 正文

如何正确实现Android启动屏画面的方法(避免白屏)

2019-10-22 18:14:36
字体:
来源:转载
供稿:网友

Android启动屏不正确的实现可能会导致用户长时间等待,或者可能会出现黑白屏。这里简单演示如何正确实现Android启动屏。

演示分为以下几个步骤:

  1. 在res/drawable文件夹中创建splash_background.xml文件。
  2. 编辑res/values/styles.xml
  3. 创建java/.../SplashActivity
  4. 编辑manifests/AndroidManifest.xml

1、在res/drawable文件夹中创建splash_background.xml文件

根据你的需求调整位图图像的重力和尺寸。

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">  <item android:drawable="@color/colorPrimary"/>  <item android:gravity="center" android:width="100dp" android:height="100dp">    <bitmap      android:gravity="fill_horizontal|fill_vertical"      android:src="@drawable/logo"/>  </item></layer-list>

2、编辑res/values/styles.xml

这里的样式用于启动画面。 这是为了在启动屏幕时隐藏操作栏。

<resources>  <!-- Base application theme. -->  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">    <!-- Customize your theme here. -->    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="colorAccent">@color/colorAccent</item>  </style>  <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">    <item name="android:windowBackground">@drawable/splash_background</item>  </style></resources>

3、创建java/.../SplashActivity

一旦App启动,SplashActivity将启动,然后转移到MainActivity。

package com.example.jtdan.goodSplash;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class SplashActivity extends AppCompatActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //switch from splash activity to main activity    Intent intent = new Intent(this, MainActivity.class);    startActivity(intent);    finish();  }}

4、编辑manifests/AndroidManifest.xml

在清单文件中添加新的启动画面Activity。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.example.jtdan.goodSplash">  <application    android:allowBackup="true"    android:icon="@mipmap/ic_launcher"    android:label="goodSplash"    android:roundIcon="@mipmap/ic_launcher_round"    android:supportsRtl="true"    android:theme="@style/AppTheme">    <activity android:name="com.example.jtdan.goodSplash.SplashActivity" android:theme="@style/SplashTheme">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>    <activity android:name="com.example.jtdan.goodSplash.MainActivity"></activity>  </application></manifest>

示例源码地址:https://github.com/mrjoedang/goodSplash

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表