using Android.App;
using Android.Content.PM;
using Android.OS;
using AndroidX.Core.View;
namespace Hua.Todo.Maui;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
///
/// Android 平台入口 Activity。
///
public class MainActivity : MauiAppCompatActivity
{
///
/// Activity 创建时回调。
///
/// 上次保存状态。
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
ConfigureSystemBars();
#if DEBUG
Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);
#endif
}
///
/// Activity 恢复时回调。
/// 某些设备/启动链路会在恢复阶段重置系统栏样式,这里再次应用。
///
protected override void OnResume()
{
base.OnResume();
ConfigureSystemBars();
}
///
/// 窗口焦点变化回调。
/// 焦点切换后部分系统会重新应用主题色,需再次修正。
///
/// 是否获得焦点。
public override void OnWindowFocusChanged(bool hasFocus)
{
base.OnWindowFocusChanged(hasFocus);
if (hasFocus)
{
ConfigureSystemBars();
}
}
///
/// 配置 Android 系统栏样式,避免默认紫色状态栏与页面视觉割裂。
///
private void ConfigureSystemBars()
{
if (Window is null)
{
return;
}
// 使用接近页面背景的浅色系统栏,弱化顶部挖孔区域的突兀感。
var systemBarColor = global::Android.Graphics.Color.ParseColor("#F8FAFC");
Window.SetStatusBarColor(systemBarColor);
Window.SetNavigationBarColor(systemBarColor);
// 保持内容在系统栏下方,避免顶部控件贴到状态栏区域。
WindowCompat.SetDecorFitsSystemWindows(Window, true);
var insetsController = WindowCompat.GetInsetsController(Window, Window.DecorView);
if (insetsController is not null)
{
insetsController.AppearanceLightStatusBars = true;
insetsController.AppearanceLightNavigationBars = true;
}
}
}