You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI实现双Logo启动页遇异常,求解决方案

解决MAUI双Logo启动页Android平台集成的布局异常问题

问题根源

直接在MainActivity.OnCreate中调用SetContentView设置启动页布局,会覆盖MAUI默认生成的根布局,导致MAUI导航框架初始化时找不到必需的navigationlayout_content容器,从而抛出Java.Lang.IllegalArgumentException异常。

正确实现步骤

1. 保留MAUI默认启动流程

不要在MainActivity.OnCreate中手动调用SetContentView,MAUI会自动处理应用根布局的初始化。

2. 编写双Logo启动页布局

在Platforms/Android/Resources/layout下创建splash_screen.xml,用RelativeLayout实现居中+页脚的双Logo布局:

<?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"
    android:background="@color/white">

    <!-- 居中显示的主Logo -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/icon1" />

    <!-- 页脚显示的次Logo -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="32dp"
        android:src="@drawable/icon2" />
</RelativeLayout>

3. 定义启动页主题

在Platforms/Android/Resources/values/styles.xml中创建SplashTheme,仅通过windowBackground关联自定义布局,继承MAUI默认主题以保留基础配置:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SplashTheme" parent="Theme.Maui.NoActionBar">
        <item name="android:windowBackground">@layout/splash_screen</item>
        <item name="android:windowFullscreen">true</item> <!-- 可选:设置全屏启动页 -->
    </style>
</resources>

4. 配置AndroidManifest.xml

在application和MainActivity标签中指定启动主题,确保应用启动时先加载自定义启动页:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/appicon" 
    android:roundIcon="@mipmap/appicon_round" 
    android:supportsRtl="true" 
    android:theme="@style/SplashTheme">
    
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

5. 清理错误代码

打开Platforms/Android/MainActivity.cs,删除手动设置布局的代码,保留MAUI默认初始化逻辑:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    // 移除所有SetContentView相关调用
}

内容的提问来源于stack exchange,提问作者Matthew Pans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:05:08