如何在 .NET MAUI 中修改安卓状态栏和导航栏颜色

本教程指导您如何在 .NET MAUI 安卓应用中通过修改 colors.xml 和 MainActivity.cs 中的主题来自定义状态栏和导航栏的背景颜色。

  1. colors.xml文件中添加style

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    <resources>
        <color name="colorPrimary">#512BD4</color>
        <color name="colorPrimaryDark">#2B0B98</color>
        <color name="colorAccent">#2B0B98</color>
    
        <style name="SplashTheme" parent="@style/Maui.SplashTheme">
            <item name="android:statusBarColor">@android:color/black</item>
            <item name="android:navigationBarColor">@android:color/black</item>
        </style>
    </resources>
    
  2. MainActivity.cs(Platforms/Android)文件中修改主题。

    1
    2
    3
    4
    5
    
    [Activity(
        //Theme = "@style/Maui.SplashTheme",
        Theme = "@style/SplashTheme",
        MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
    public class MainActivity : MauiAppCompatActivity
    
Built with Hugo
Theme Stack designed by Jimmy