Android 防止白屏与华为手机欢迎页面全屏时设置背景拉伸图片

本文介绍如何在Android项目中配置欢迎页,包括定义欢迎页背景、创建主题样式及在Manifest文件中引用主题。此外还介绍了如何设置虚拟按键NavigationBar的颜色。

1、在drawable里面书写欢迎页面的背景splash_bg文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/black" />
    </item>
    <item>
        <bitmap
            android:dither="true"
            android:filter="true"
            android:gravity="fill"
            android:src="@drawable/welcome"
            android:tileMode="disabled" />
    </item>

</layer-list>

其中gravity为图片在window中的位置,color为应用的主题颜色

2、在style里面书写欢迎页的主题

 <!-- 欢迎界面主题 -->
    <style name="WelcomeTheme" >
    <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:background">@drawable/splash_bg</item>
    </style>

3、在Manifest里面引用该主题

<activity
            android:name=".main.activity.WelcomeActivity"
            android:theme="@style/WelcomeTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.MULTIWINDOW_LAUNCHER" />
            </intent-filter>
        </activity>

额外:设置(虚拟按键)NavigationBar的颜色

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <!--&lt;!&ndash;Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色&ndash;&gt;-->
        <!--&lt;!&ndash; 可以修改状态栏的颜色 -->
        <item name="android:statusBarColor">@color/color_main</item>
    </style>
### 华为手机上 Flutter 应用白屏问题分析解决方案 Flutter 打包 Android APK 后,在华为手机上出现白屏的问题,通常是由以下原因导致的:依赖库配置错误、主题设置不正确、资源加载失败或初始化异常等。以下是针对该问题的详细分析和解决方案。 #### 1. 检查 `AndroidManifest.xml` 中的主题配置 确保在 `AndroidManifest.xml` 文件中,应用的默认主题设置正确。如果主题未正确配置,可能会导致应用启动无法渲染界面,从而出现白屏现象。 ```xml <application android:theme="@style/Theme.AppCompat.Light.NoActionBar"> </application> ``` 此外,华为手机可能存在特定的系统优化机制,建议将主题设置为兼容性更高的样式,例如 `Theme.MaterialComponents.NoActionBar` 或 `Theme.AppCompat.Light.NoActionBar`[^1]。 #### 2. 配置正确的依赖库版本 检查项目中的依赖库版本是否华为设备兼容。例如,`flutter_plugin_android_lifecycle` 或 `flutter_boost` 等插件可能需要更新到最新版本以支持华为设备的特性。 在 `pubspec.yaml` 文件中,确保使用了稳定的 Flutter 插件版本: ```yaml dependencies: flutter: sdk: flutter flutter_plugin_android_lifecycle: ^2.0.5 ``` 同,在 `build.gradle` 文件中,确保使用了兼容的 Gradle 版本和 Android 插件版本: ```gradle buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } ``` #### 3. 检查资源文件的完整性 华为手机上的白屏问题也可能由资源文件加载失败引起。例如,图片资源路径错误或分辨率不匹配可能导致应用无法正常渲染界面。 确保所有资源文件(如图片、字体等)已正确添加到 `res` 目录下,并且路径名称符合规范(避免使用特殊字符或大写字母)。如果使用了自定义的启动页背景,需确认其兼容性: ```xml <ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_splash_snow" /> ``` 如果背景图片较大,建议优化图片尺寸和格式,以减少加载间[^2]。 #### 4. 初始化逻辑优化 某些情况下,Flutter 应用在初始化阶段可能出现异常,导致界面卡在空白状态。可以通过以下方式排查和解决: - **检查主入口函数**:确保 `main()` 函数中没有阻塞操作或未捕获的异常。 ```dart void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(MyApp()); } ``` - **启用调试模式**:通过日志工具(如 `Logcat`)查看是否存在初始化阶段的错误信息。 - **延迟加载**:如果应用启动需要加载大量数据,可以考虑使用异步加载或分步加载的方式,避免主线程阻塞。 #### 5. 兼容华为 HMS 服务 华为设备可能禁用了 Google 服务框架,因此需要确保应用兼容华为 HMS(Huawei Mobile Services)。在打包 APK ,可以选择生成支持 HMS 的独立 APK。 在 `build.gradle` 文件中添加 HMS SDK 依赖: ```gradle dependencies { implementation 'com.huawei.hms:hianalytics:6.4.0.300' } ``` 同,确保在华为应用市场中完成相关配置,例如 HMS AppGallery Connect 的集成。 --- ### 示例代码:优化启动页加载逻辑 以下是一个优化后的启动页加载逻辑示例,避免因初始化问题导致白屏: ```dart import 'package:flutter/material.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: SplashPage(), debugShowCheckedModeBanner: false, ); } } class SplashPage extends StatefulWidget { @override _SplashPageState createState() => _SplashPageState(); } class _SplashPageState extends State<SplashPage> { @override void initState() { super.initState(); Future.delayed(Duration(seconds: 2), () { Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HomePage()), ); }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Center( child: Image.asset('assets/images/splash.png'), // 确保图片路径正确 ), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home')), body: Center(child: Text('Welcome to the app!')), ); } } ``` --- ###
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值