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

Material BottomNavigationView弹出键盘时图标消失问题求助

问题:BottomAppBar内嵌BottomNavigationView时,键盘弹出后菜单图标消失

我实现了一个内嵌BottomNavigationView的Material BottomAppBar,遇到异常行为:当所在Activity配置android:windowSoftInputMode="adjustResize",键盘弹出导致BottomAppBar移到键盘上方时,栏上的菜单图标会消失;键盘隐藏后图标恢复正常。

键盘弹出时BottomNavigationView图标消失

布局代码如下:

<androidx.coordinatorlayout.widget.CoordinatorLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/parentCoordinatorLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

.....
    <com.google.android.material.bottomappbar.BottomAppBar
       android:id="@+id/bottomAppBar"
       android:layout_width="match_parent"
       android:layout_height="60dp"
       app:fabCradleMargin="10dp"
       app:fabCradleRoundedCornerRadius="0dp"
       app:fabCradleVerticalOffset="10dp">

       <com.google.android.material.bottomnavigation.BottomNavigationView
           android:id="@+id/bottomNavigationView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_marginEnd="16dp"
           app:menu="@menu/bottom_navigation" />

    </com.google.android.material.bottomappbar.BottomAppBar>

   <com.google.android.material.floatingactionbutton.FloatingActionButton
       android:id="@+id/profileCloseFab"
       android:layout_width="57dp"
       android:layout_height="57dp"
       android:contentDescription="@string/app_name"
       app:borderWidth="0dp"
       android:elevation="2dp"
       android:src="@drawable/ic_bottom_menu_fab_person_gray"
       app:layout_anchor="@id/bottomAppBar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout >

解决思路

  • 避免直接嵌套BottomNavigationView:BottomAppBar的测量逻辑和Fab的cradle布局可能在resize时干扰子View绘制。建议将BottomNavigationView独立于BottomAppBar,用CoordinatorLayout的约束让两者对齐;如果需求允许,直接使用BottomAppBar自带的app:menu属性替代内嵌BottomNavigationView。
  • 调整BottomAppBar高度:不要硬设android:layout_height="60dp",改用wrap_content或Material组件默认高度,硬设高度可能导致resize时子View空间计算错误。
  • 排查Fab相关属性:暂时移除fabCradleMargin、fabCradleRoundedCornerRadius、fabCradleVerticalOffset这些属性,确认是否是Fab布局逻辑引发的冲突,再逐步恢复调整。
  • 强制视图重绘:通过ViewTreeObserver.OnGlobalLayoutListener监听键盘状态变化,在弹出/收起时调用bottomNavigationView.postInvalidate()或bottomNavigationView.requestLayout()强制重绘。
  • 更新Material组件版本:确保com.google.android.material:material库为最新稳定版,旧版本可能存在此类布局bug。

内容的提问来源于stack exchange,提问作者Ross Stepaniak

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 10:57:20