首页 > 解决方案 > 在 BottomNavigationView 中没有椭圆的标签

问题描述

我有一个BottomNavigationView属性app:labelVisibilityMode设置为的布局labeled

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/navigation" />

</FrameLayout>

菜单有五个项目:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_navigation"
        android:title="Short" />

    <item
        android:id="@+id/navigation_notifications_1"
        android:icon="@drawable/ic_navigation"
        android:title="Short" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_navigation"
        android:title="Longer text123" />

    <item
        android:id="@+id/navigation_notifications_2"
        android:icon="@drawable/ic_navigation"
        android:title="Short" />

    <item
        android:id="@+id/navigation_notifications_3"
        android:icon="@drawable/ic_navigation"
        android:title="Short" />

</menu>

第三个项目有问题,标签较长("Longer text123") - 第二个单词不是椭圆形的,但根本不显示:

在此处输入图像描述

使标签更短一点会导致正确的行为:

在此处输入图像描述

有没有办法处理更长的标签?最好的解决方案是省略它并在没有空间容纳整个文本时显示“Longer text...”。

标签: androidbottomnavigationview

解决方案


我有同样的问题并搜索了很多。最后我得到了一个要添加到代码中的解决方案。只需将以下行添加到您的代码中。

 BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigation.getChildAt(0);
    for (int i = 0; i < menuView.getChildCount(); i++) {
        BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
        View activeLabel = item.findViewById(R.id.largeLabel);
        if (activeLabel instanceof TextView) {
            activeLabel.setPadding(0, 0, 0, 0);
        }
    }

推荐阅读