Skip to content

fix(android): 键盘弹出时底部系统栏保持主题浸入 - #8

Merged
xiufeigo merged 1 commit into
mainfrom
cursor/android-keyboard-nav-immersion-510d
Oct 17, 2026
Merged

xiufeigo merged 1 commit into
mainfrom
cursor/android-keyboard-nav-immersion-510d

Conversation

@xiufeigo

Copy link
Copy Markdown
Owner

问题

Android 平板上点进聊天输入框、软键盘弹出后,底部手势条 / 导航栏不再浸入,和页面底色脱开。浅色主题下这条会变成纯白,和桉树浅色(约 #f4f7f5)对不上;深色主题下同样是纯白,尤其明显。

根因

导航栏被设成了 Color.TRANSPARENT,沉浸靠 decorView 的主题色从透明栏后面透出来。输入框获焦、IME inset 到来时,这条假设不成立:

  • 框架在导航栏完全透明且 IME 可见时,会改用默认浅色(纯白)把底边手势条画实。这个回退不看应用自己的暗色主题,系统日间模式时深色界面也会变白。
  • enableEdgeToEdge() 的默认 SystemBarStyle.auto 会把 isNavigationBarContrastEnforced 设为 true。三键导航因此叠上约 90% 不透明的白 scrim(DefaultLightScrim = #E6FFFFFF)。主题同步每 800ms 把导航栏重新写成透明,但没有再次关掉对比度强制,所以键盘期间白条会留住。
  • Android 15 三键导航在忽略 setNavigationBarColor 时改读 window background,并且要求它是 ColorDrawable。原先只调用 decorView.setBackgroundColor(),窗口背景仍是 Material 日间主题的白底。
  • WebView 默认底色是纯白。内容被 IME padding 顶上去之后,手势条那一截如果还露出 WebView,看到的也是白。

键盘收起时窗口全屏、透明栏盖在主题色 decorView 上,所以看起来是浸入的。问题只在获焦 / IME 出现之后。

修复

MainActivity.applySystemBars() 统一钉色,主题同步、JS bridge、inset 变化和窗口重新获焦都会再写一次:

  • 导航栏用不透明的 --color-bg-100,不再用透明。状态栏仍透明,顶部继续由页面自己画。
  • 每次都关掉 status/navigation contrast enforcement,并清掉 FLAG_TRANSLUCENT_*,避免 setNavigationBarColor 被忽略。
  • window.background、decorView、android.R.id.content 和 WebView 都铺成同一主题色,inset padding 露出的缝和 Android 15 的 window background 回退不再是白。
  • enableEdgeToEdge 改用 SystemBarStyle.light(TRANSPARENT, TRANSPARENT),避免 auto 样式把对比度强制打开。
  • setSystemBars 改到主线程执行(JavascriptInterface 不在 UI 线程)。

底部 inset 算法没变:max(IME, systemBars),键盘仍然把 WebView 顶上去。

验证

这台环境没有 Android SDK / 平板模拟器,没有做真机或模拟器点按。

已核对的路径:

  • 主题色一旦由 JS 或 800ms 轮询写入,随后的 IME inset 和 onWindowFocusChanged 会把同一不透明色再钉回去,不会被透明覆盖。
  • 浅色主题导航栏图标仍走 isAppearanceLightNavigationBars = true(深色图标);深色主题为 false(浅色图标)。判定与原来的 isColorLight(color) && mode != "dark" 一致。
  • 键盘收起时底边 padding 仍是导航栏高度,栏色等于原来透出的 decorView 主题色,关闭键盘的浸入外观不变。
  • 状态栏颜色保持透明,顶部 safe-area 仍由 Web 绘制。

建议在平板上再看一眼:浅色 / 深色各聚焦一次输入框,手势条应与页面底色连成一块,深色模式不能是纯白;收起键盘后状态栏和底栏仍浸入。三键导航也看一下,确认没有白 scrim。

Open in Web Open in Cursor 

A fully transparent navigation bar is replaced by the framework default
(white, not the app theme) once an input is focused and the IME is shown.
On tablets the gesture handle stays visible, so dark mode turns that strip
pure white. Pin an opaque theme color, disable contrast enforcement, and
reapply it when insets or window focus change.

Co-authored-by: xiufeigo <xiufeigo@users.noreply.github.com>
@xiufeigo
xiufeigo marked this pull request as ready for review October 17, 2026 06:40
@xiufeigo
xiufeigo merged commit 2c0e832 into main Oct 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants