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

MAUI开发:如何获取Android系统导航栏高度?

在MAUI中获取系统导航栏高度的解决方案

因为MAUI是跨平台框架,需要针对不同平台分别实现系统导航栏高度的获取逻辑,以下是各平台的具体实现方式:

1. 定义共享服务接口

首先在MAUI共享项目中定义一个接口,统一导航栏高度的获取方法:

public interface INavigationBarHeightService
{
    int GetNavigationBarHeight();
}

2. 各平台实现服务

Android平台

Android通过系统资源或WindowInsets获取导航栏高度,兼容新旧版本:

[assembly: Dependency(typeof(NavigationBarHeightService))]
namespace YourAppName.Platforms.Android
{
    public class NavigationBarHeightService : INavigationBarHeightService
    {
        public int GetNavigationBarHeight()
        {
            var currentActivity = Platform.CurrentActivity;
            if (currentActivity == null) return 0;

            // 优先使用WindowInsets获取实际显示的导航栏高度(适配导航栏隐藏/显示状态)
            var windowInsets = currentActivity.Window.DecorView.RootWindowInsets;
            if (windowInsets != null)
            {
                var insets = WindowInsetsCompat.Type.NavigationBars().Apply(windowInsets);
                return insets.Bottom;
            }

            // 兼容旧版本的 fallback 方案
            var context = Android.App.Application.Context;
            int resourceId = context.Resources.GetIdentifier("navigation_bar_height", "dimen", "android");
            return resourceId > 0 ? context.Resources.GetDimensionPixelSize(resourceId) : 0;
        }
    }
}

iOS平台

iOS没有固定的系统导航栏,底部的安全区域高度就是需要避开的区域,直接获取安全区底部值即可:

[assembly: Dependency(typeof(NavigationBarHeightService))]
namespace YourAppName.Platforms.iOS
{
    public class NavigationBarHeightService : INavigationBarHeightService
    {
        public int GetNavigationBarHeight()
        {
            // iOS 11及以上支持安全区
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                var keyWindow = UIApplication.SharedApplication.ConnectedScenes
                    .OfType<UIWindowScene>()
                    .FirstOrDefault()?.Windows
                    .FirstOrDefault(w => w.IsKeyWindow);
                
                return keyWindow != null ? (int)keyWindow.SafeAreaInsets.Bottom : 0;
            }
            return 0;
        }
    }
}

Windows平台

通过对比屏幕总高度和应用可见区域高度,差值即为系统导航栏高度:

[assembly: Dependency(typeof(NavigationBarHeightService))]
namespace YourAppName.Platforms.Windows
{
    public class NavigationBarHeightService : INavigationBarHeightService
    {
        public int GetNavigationBarHeight()
        {
            var appView = ApplicationView.GetForCurrentView();
            if (appView == null) return 0;

            var screenInfo = DisplayInformation.GetForCurrentView();
            var screenHeight = screenInfo.ScreenBounds.Height;
            var visibleHeight = appView.VisibleBounds.Height;

            return (int)(screenHeight - visibleHeight);
        }
    }
}

3. 在MAUI中调用服务

在共享项目的业务代码中,通过DependencyService获取实例并调用,得到高度后可设置底部内容的Margin或Padding,避免被导航栏覆盖:

// 获取导航栏高度(像素单位)
var navBarHeight = DependencyService.Get<INavigationBarHeightService>()?.GetNavigationBarHeight() ?? 0;

// 示例:设置底部标签的底部边距
versionLabel.Margin = new Thickness(0, 0, 0, navBarHeight);

内容的提问来源于stack exchange,提问作者Gold.strike

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 11:05:26