BottomNavigationView高度异常:图标无法垂直居中问题求助
问题现象
使用Kotlin+XML开发Android应用时,BottomNavigationView图标无法垂直居中:Android Studio预览显示正常,但模拟器中控件高度变大,图标偏上。
相关代码
主布局XML
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="0dp" android:layout_height="0dp" app:defaultNavHost="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/main_activity_nav" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:layout_marginBottom="20dp" android:background="@drawable/nav_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/bottom_nav_menu" app:itemIconSize="28dp" app:labelVisibilityMode="unlabeled" app:itemIconTint="@drawable/nav_item_selector" app:itemBackground="@drawable/nav_item_background" app:itemRippleColor="@null" app:itemActiveIndicatorStyle="@null" /> </androidx.constraintlayout.widget.ConstraintLayout>
nav_background.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <corners android:radius="100dp"/> <solid android:color="@color/white"/> </shape> </item> </selector>
已尝试无效方案
- 设置固定高度(56dp/72dp)导致图标被截断
- 检查Drawable资源内边距,无异常设置
- 调整
itemIconSize数值,问题未解决 - 保留
wrap_content高度,图标仍偏上
显示效果
- 模拟器显示:

- Android Studio预览:

解决方案
方案1:移除Item垂直内边距(优先推荐)
在BottomNavigationView中添加app:itemVerticalPadding="0dp",强制移除item默认的上下内边距,让图标自动垂直居中:
<com.google.android.material.bottomnavigation.BottomNavigationView ... app:itemVerticalPadding="0dp" /> <!-- 新增该行 -->
方案2:自定义Item布局
若方案1无效,创建自定义item布局确保图标完全居中:
- 新建
layout/bottom_nav_item.xml:
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="4dp" />
- 在BottomNavigationView中引用该布局:
app:itemLayout="@layout/bottom_nav_item"
方案3:强制控制控件高度与内边距
设置固定高度并调整内部padding,平衡高度与图标显示:
<com.google.android.material.bottomnavigation.BottomNavigationView ... android:layout_height="56dp" android:paddingVertical="8dp" />
内容的提问来源于stack exchange,提问作者Ali S.
相关产品推荐
相关产品推荐

