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

如何去除Android BottomNavigationView中图标的默认边距?

问题:去除BottomNavigationView图标默认边距

已通过app:labelVisibilityMode="unlabeled"成功隐藏导航项文字,但始终无法消除图标的默认边距。

布局代码

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:labelVisibilityMode="unlabeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_menu"/>

</androidx.constraintlayout.widget.ConstraintLayout>

菜单文件代码

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/reproductive"
        android:icon="@drawable/baseline_calendar_month_24"
        android:title="Dashboard"/>
    <item
        android:id="@+id/dashboard"
        android:icon="@drawable/baseline_info_24"
        android:title="Reprodução"/>
</menu>

已尝试无效的方法

  • 在BottomNavigationView的XML中设置android:layout_margin="0dp",边距仍存在;
  • 为BottomNavigationView创建自定义样式并设置android:layout_margin,无效;
  • 在BottomNavigationView的XML中同时设置android:padding="0dp"和android:layout_margin="0dp",问题未解决。

当前效果

当前效果

期望效果

期望效果

有效解决方案

方案1:使用Material组件内置属性直接控制

在BottomNavigationView的XML中添加两个属性,直接控制导航项的内边距:

<com.google.android.material.bottomnavigation.BottomNavigationView
    ...
    app:itemPaddingHorizontal="0dp"
    app:itemPaddingVertical="0dp"/>

方案2:自定义样式全局控制

在res/values/styles.xml中创建自定义样式:

<style name="CustomBottomNavStyle" parent="Widget.MaterialComponents.BottomNavigationView">
    <item name="itemPaddingHorizontal">0dp</item>
    <item name="itemPaddingVertical">0dp</item>
</style>

然后在布局中给BottomNavigationView应用该样式:

<com.google.android.material.bottomnavigation.BottomNavigationView
    ...
    style="@style/CustomBottomNavStyle"/>

方案3:代码动态修改内边距

如果XML属性不生效,可通过代码遍历导航项修改:

val bottomNav = findViewById<BottomNavigationView>(R.id.your_bottom_nav_id)
val menuView = bottomNav.getChildAt(0) as BottomNavigationMenuView
for (i in 0 until menuView.childCount) {
    val itemView = menuView.getChildAt(i)
    itemView.setPadding(0, 0, 0, 0)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 00:03:19