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"
) - 第二个单词不是椭圆形的,但根本不显示:
使标签更短一点会导致正确的行为:
有没有办法处理更长的标签?最好的解决方案是省略它并在没有空间容纳整个文本时显示“Longer text...”。
解决方案
我有同样的问题并搜索了很多。最后我得到了一个要添加到代码中的解决方案。只需将以下行添加到您的代码中。
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);
}
}
推荐阅读
- angular - 在 Angular 微应用元素之间共享库
- prometheus - 1d 时间的总和,限制为最后 7d,计数器作为值
- javascript - 无法获取/file1
- java - 如何将 React 构建的文件提供给 Spring Boot 应用程序
- splunk - Splunk:如何为 pathparam 编写正则表达式?
- python - List Comprehension Appending Index with Number Range on Lists
- excel - 找到达到配额目标所需的增长百分比。Excel
- java - 如何优化使用嵌套 for 循环的代码?
- ranorex - 如何在 Ranorex 中使用全局变量?
- python - 如何将字节添加在一起并根据python中的校验和验证它们