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

Android平台MAUI全屏应用键盘位置调整及底部黑条问题解决

MAUI全屏模式下键盘弹出底部黑条问题解决方案

迁移Xamarin.Forms至MAUI后,全屏模式下点击Entry触发键盘弹出时,底部出现原安卓导航栏区域的黑条,该问题在Xamarin.Forms中不存在。以下是针对性解决步骤:

1. 调整MainActivity的全屏与键盘配置

修改MainActivity.cs,在OnCreate方法中添加窗口布局与键盘模式设置,同时保留原有的沉浸式全屏逻辑:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // 让窗口布局延伸至整个屏幕,无边界限制
    Window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
    // 清除半透明状态栏标记,设置状态栏透明
    Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
    Window.SetStatusBarColor(Android.Graphics.Color.Transparent);

    // 设置键盘弹出模式:平移窗口而非调整大小,避免预留导航栏空间
    Window.SetSoftInputMode(SoftInput.AdjustPan | SoftInput.StateHidden);
}

public override void OnWindowFocusChanged(bool hasFocus)
{
    base.OnWindowFocusChanged(hasFocus);
    if (hasFocus)
    {
        var uiOptions = SystemUiFlags.HideNavigation | 
                        SystemUiFlags.LayoutHideNavigation | 
                        SystemUiFlags.LayoutFullscreen | 
                        SystemUiFlags.Fullscreen | 
                        SystemUiFlags.LayoutStable | 
                        SystemUiFlags.ImmersiveSticky;
        Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
    }
}

关键说明:

  • WindowManagerFlags.LayoutNoLimits:确保应用窗口完全覆盖屏幕,包括原导航栏所在的底部区域,避免系统预留空白。
  • SoftInput.AdjustPan:键盘弹出时直接平移页面内容,而非调整窗口高度,从根源上消除底部黑条的产生。

2. 验证主题配置(可选)

检查Platforms/Android/Resources/values/styles.xml中的主题设置,确保启用全屏属性:

<style name="MainTheme" parent="MainTheme.Base">
    <!-- 强制全屏显示 -->
    <item name="android:windowFullscreen">true</item>
    <!-- 允许内容延伸至屏幕刘海/打孔区域(若有) -->
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

完成上述修改后重新编译运行,点击Entry控件时,键盘将直接显示在屏幕底部,底部黑条问题即可解决。

内容的提问来源于stack exchange,提问作者Ken K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 23:15:33