.Net MAUI 安卓状态栏透明、半透明、全屏

[删除(380066935@qq.com或微信通知)]

更好的阅读体验请查看原文:https://blog.csdn.net/sswi321/article/details/125208149

测试机器为:美帝良心想 padpro11,系统版本:安卓11

开发环境:Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.3.6

MAUI版本:6.0.541

Demo 源码

方法:

一、在Platforms→Resources→values文件夹下创建一个style.xml文件,里面包含一个资源主题名字叫 MainTheme,名字自己取, "继承"于MAUI默认的启动主题即parent = Maui.SplashTheme,然后写入三个状态栏、导航栏配置,如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="MainTheme" parent="Maui.SplashTheme">
		<item name="android:windowTranslucentStatus">false</item>
		<item name="android:windowTranslucentNavigation">true</item>
		<item name="android:statusBarColor">@android:color/transparent</item>
	</style>
</resources>

VS中的位置:

截图:

二、打开Platforms→MainActivity.cs文件,

1.更改特性

//原 [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
//改为下面Theme = "@style/(你创建主题的名字)"
[Activity(Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]

2.重写OnCreate方法即可

        protected override void OnCreate(Bundle savedInstanceState)
        {
            //全屏,即隐藏状态栏,时间、信号这些也不可见
            // Window.SetFlags(Android.Views.WindowManagerFlags.Fullscreen, Android.Views.WindowManagerFlags.Fullscreen);

            //半透明 任务栏
            // Window.SetFlags(Android.Views.WindowManagerFlags.TranslucentStatus, Android.Views.WindowManagerFlags.TranslucentStatus);

            //全透明任务栏
            Window.SetFlags(Android.Views.WindowManagerFlags.TranslucentNavigation, Android.Views.WindowManagerFlags.TranslucentNavigation);


            //设置状态栏、导航栏色颜色为透明
            Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
            Window.SetNavigationBarColor(Android.Graphics.Color.Transparent);

            base.OnCreate(savedInstanceState);
        }

截图:

VS中文件:

三、运行效果:

1.MAUI默认状态:

启动画面(有状态栏),程序运行状态(有状态栏)


2.状态栏全透明

启动画面 (有状态栏,且透明) ,程序运行状态(有状态栏,且透明)

3.全屏无状态栏