android - BottomNavigationView 中的标签未省略

标签 android 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”) - 第二个单词没有省略,只是不显示:

enter image description here

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

enter image description here

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

最佳答案

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

 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);
        }
    }

关于android - BottomNavigationView 中的标签未省略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52698872/

相关文章:

java - 如何使用 Android Studio 中的 Activity 而不是 Fragment 更改 BottomNavigationView 中所选图标的颜色?

android - 如何在android中显示带有图像的html页面?

java - Android 中的自动登录

java - 在 fragment 之间更改时底部导航栏会向下推

android - 如何将tablayout与底部导航和viewpager一起使用?

android - BottomNavigationView 中的选定项与背景颜色相同

android - 如何像在 TabLayout 中一样在 BottomNavigationView 中获取选定的标签行?

android - 如何构建 renderscipt 相机预览

android - 如何使用PhoneGap读写内部数据库?

java - 如何从for循环中依次显示每个值