移除BottomNavigationView图标并将标题居中

5

我有一个底部导航栏,其中包含3个项目。我想要每个标签都仅有居中的文本,因此希望完全删除图标(不只是使它们透明)。

如何删除图标并将标题居中?

这是我目前的情况: enter image description here 这是我想要的效果: enter image description here

我的代码: (更喜欢在XML中提供解决方案)

<merge  xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/navigationBar"
            android:background="@color/navigation"
            app:theme="@style/BottomNavigationTheme"
            app:menu="@menu/bottom_navigation_menu"
            android:minHeight="@dimen/abc_action_bar_default_height_material">

        </com.google.android.material.bottomnavigation.BottomNavigationView>

    </RelativeLayout>

</merge>

bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu   xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/ic_home"
        android:title="@string/home">
    </item>

    <item
        android:id="@+id/ic_today"
        android:title="@string/today">
    </item>

    <item
        android:id="@+id/ic_you"
        android:title="@string/you">
    </item>

</menu>
4个回答

3
最简单的方法就是直接使用。
android:paddingBottom="16dp" //(any dp you want)
android:clipToPadding="false"

0

这对我来说很有效

private int baselineHeight = 0;

private void removeIcons(BottomNavigationView view) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
    for (int i = 0; i < menuView.getChildCount(); i++) {
        BottomNavigationItemView itemView = (BottomNavigationItemView) (menuView.getChildAt(i));
        BaselineLayout baseline = (BaselineLayout) itemView.getChildAt(1);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) baseline.getLayoutParams();
        baselineHeight = baselineHeight > 0 ? baselineHeight : (menuView.getHeight() + baseline.getHeight()) / 2;
        layoutParams.height = baselineHeight;
        baseline.setLayoutParams(layoutParams);
    }
}

只需在您的Activity中的onCreate()中调用它,并将您的BottomNavigationView作为参数传递。

如果您不想在Activities或Fragments中添加过多的代码,而且您希望在您的layout XML中存在此代码,则可以创建一个自定义View,该视图扩展了BottomNavigationView并在onLayout()覆盖中调用此函数。


0

为您的底部面板添加固定高度并设置底部填充。在我的情况下有效。

   android:layout_height="35dp"
   android:paddingBottom="20dp"
   android:clipToPadding="false"

-1

您可以使用底部导航视图的此属性来隐藏文本,它将自动居中您的图标,我认为您没有使用minHeight属性先生。 app:labelVisibilityMode="unlabeled"


谢谢您的回答,但我认为您误解了,我试图隐藏图标并将文本居中。而不是相反。您的解决方案会删除我想要保留的文本。 - Joe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接