我有一个带有 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”
) - 第二个单词没有省略,只是不显示:
使标签短一点会导致正确的行为:
有没有办法处理更长的标签?最好的解决方案是在没有空间容纳整个文本时将其省略并显示“更长的文本...”。
最佳答案
我也遇到了同样的问题,并进行了很多搜索。最后我得到了一个要在代码中添加的解决方案。 只需将以下行添加到您的代码中即可。
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/