Android app启动闪屏主题设置

xiaoxiao2021-02-28  114

以农行掌上银行为例我们来谈谈应用启动后闪白屏,若干秒之后进入Splash界面(欢迎页面)的问题。

其实这种问题不只是农行APP的专利,其它很多APP都有这种问题,默认不对SplashActivity(启动应用的Activity)的主题背景做处理都是白色的,除非为application的主题设置了背景色。可是有很多APP它们启动之后从来没有闪过一下白屏(肉眼分辨不出有闪白屏):

支付宝和网易有道词典很明显没有闪出白屏的问题,给人的感觉是点击应用图标之后,应用马上响应、加载闪屏logo进入应用主界面。

如何让我们的应用和支付宝、网易有道词典一样点击之后就马上响应呢?

其实很简单:

<application android:allowBackup="true" android:icon="@mipmap/logo" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".SplashActivity" android:theme="@style/launch_theme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".MainActivity"> </activity> </application>我们为启动应用的SplashActivity添加一个theme主题,launch_theme: <style name="launch_theme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="android:windowBackground">@drawable/launch_bg</item> </style>重点就在于windowBackground属性,launch_bg.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <item> <shape android:shape="rectangle"> <solid android:color="@color/colorFFFFFF"/> </shape> </item> <item> <bitmap android:gravity="center|bottom" android:src="@drawable/splash_pic"/> </item> </layer-list>splash_pic.png闪屏图标如下图:

SplashActivity对应的布局文件activity_splash.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_splash" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorFFFFFF"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:src="@drawable/splash_pic"/> </RelativeLayout>

activity_splash.xml页面效果与launch_bg.xml页面效果一模一样,用户点击应用图标后看到的是我们设置的两个相同的页面(感觉是一个页面),之后进入主页面。

如此设置之后,我们来看看启动效果图:

处理应用启动后白屏的问题核心是修改启动应用的Activity的theme属性,各应用可以根据自身应用的风格样式设计修改,给用户的感觉舒服自然就好。

转载请注明原文地址: https://www.6miu.com/read-40999.html

最新回复(0)