返回

如何在 Android 底部导航栏中只为图标添加背景?

Android

如何只为 Android 底部导航栏中的图标添加背景

作为一名资深的 Android 开发人员,我经常需要定制应用程序的用户界面。最近,我遇到了一个看似简单却让我百思不得其解的问题:如何在底部导航栏中只为图标添加背景,而不是所有项目。

问题

我想要的是一个像下面这样,只有图标有背景的底部导航栏:

[图片底部导航栏,只有图标有背景,文字没有背景]

然而,当我尝试使用 app:itemBackground 属性时,它会填充整个项目,包括图标和文本。

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/surface_container_lowest"
    app:itemTextAppearanceActive="@style/Theme.FarmKeep.TextAppearance.SemiBold.LabelSmallProminent"
    app:itemTextAppearanceInactive="@style/Theme.FarmKeep.TextAppearance.Regular.LabelSmall"
    app:itemTextColor="@color/on_surface"
    app:labelVisibilityMode="labeled"
    app:itemBackground="@drawable/selector_bottom_nav_background"
    app:menu="@menu/menu_main"/>

解决方案

经过一番研究,我找到了一个优雅的方法来解决这个问题,那就是使用自定义 XML 背景选择器和图标背景。

步骤 1:创建自定义 XML 背景选择器

创建一个名为 your_icon_background_selector.xml 的 XML 文件,内容如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/your_icon_background_pressed" />
    <item android:state_enabled="true" android:drawable="@drawable/your_icon_background_normal" />
</selector>

其中 your_icon_background_pressedyour_icon_background_normal 是您自定义的图标背景资源。

步骤 2:将自定义选择器应用到 BottomNavigationView

app:itemIconBackground 属性应用到 BottomNavigationView,并将其值设置为您创建的 XML 选择器:

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/surface_container_lowest"
    app:itemTextAppearanceActive="@style/Theme.FarmKeep.TextAppearance.SemiBold.LabelSmallProminent"
    app:itemTextAppearanceInactive="@style/Theme.FarmKeep.TextAppearance.Regular.LabelSmall"
    app:itemTextColor="@color/on_surface"
    app:labelVisibilityMode="labeled"
    app:itemIconBackground="@drawable/your_icon_background_selector"
    app:menu="@menu/menu_main"/>

步骤 3:定义图标背景

在您的 Drawable 资源文件中,创建一个名为 your_icon_background.xml 的 XML 文件,内容如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/your_icon_background_color" />
    <corners android:radius="@dimen/icon_background_radius" />
</shape>

其中 @color/your_icon_background_color 是您图标背景的颜色,@dimen/icon_background_radius 是图标背景的圆角半径。

步骤 4:调整图标大小以填充背景

为了让图标填满图标背景,请将 app:itemIconSize 属性应用到 BottomNavigationView 并设置一个适当的值:

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/surface_container_lowest"
    app:itemTextAppearanceActive="@style/Theme.FarmKeep.TextAppearance.SemiBold.LabelSmallProminent"
    app:itemTextAppearanceInactive="@style/Theme.FarmKeep.TextAppearance.Regular.LabelSmall"
    app:itemTextColor="@color/on_surface"
    app:labelVisibilityMode="labeled"
    app:itemIconTint="@color/on_surface"
    app:itemIconSize="24dp"
    app:menu="@menu/menu_main"/>

结论

通过遵循这些步骤,您可以在只填充图标的情况下为底部导航栏添加背景。这种技术对于创建具有视觉吸引力和易于使用的用户界面非常有用。

常见问题解答

  1. 为什么我需要创建自定义 XML 背景选择器?

创建自定义 XML 背景选择器可以使您自定义图标背景在不同状态(例如按下和未按下)下的外观。

  1. 我可以使用图像文件作为图标背景吗?

是的,您可以使用图像文件作为图标背景。只需在 XML 选择器中使用 android:drawable 属性指定图像。

  1. 我可以将这种技术应用于其他控件吗?

是的,这种技术不仅适用于底部导航栏,还可以应用于其他控件,例如按钮和切换按钮。

  1. 这种方法在所有 Android 版本上都兼容吗?

这种方法在 API 21 及更高版本的 Android 设备上兼容。

  1. 这种方法是否有任何缺点?

这种方法的缺点是它需要额外的资源文件和代码。但是,为了实现定制的外观,这些额外的工作是值得的。