79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
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)]
|
|
/// <summary>
|
|
/// Android 平台入口 Activity。
|
|
/// </summary>
|
|
public class MainActivity : MauiAppCompatActivity
|
|
{
|
|
/// <summary>
|
|
/// Activity 创建时回调。
|
|
/// </summary>
|
|
/// <param name="savedInstanceState">上次保存状态。</param>
|
|
protected override void OnCreate(Bundle? savedInstanceState)
|
|
{
|
|
base.OnCreate(savedInstanceState);
|
|
|
|
ConfigureSystemBars();
|
|
|
|
#if DEBUG
|
|
Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activity 恢复时回调。
|
|
/// 某些设备/启动链路会在恢复阶段重置系统栏样式,这里再次应用。
|
|
/// </summary>
|
|
protected override void OnResume()
|
|
{
|
|
base.OnResume();
|
|
ConfigureSystemBars();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 窗口焦点变化回调。
|
|
/// 焦点切换后部分系统会重新应用主题色,需再次修正。
|
|
/// </summary>
|
|
/// <param name="hasFocus">是否获得焦点。</param>
|
|
public override void OnWindowFocusChanged(bool hasFocus)
|
|
{
|
|
base.OnWindowFocusChanged(hasFocus);
|
|
if (hasFocus)
|
|
{
|
|
ConfigureSystemBars();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 配置 Android 系统栏样式,避免默认紫色状态栏与页面视觉割裂。
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|