如何强制BottomNavigationView标签多行显示

3

我有一个包含很长标签文本的 BottomNavigationView,但是它没有显示完整的标签。

我发现 BottomNavigationView 的标签使用了 maxLines="1" 属性,但我不知道如何将其修改为允许两行标签。


发布你的代码! - mohammadReza Abiri
我没有对常规的BottomNavigation代码进行任何更改。因为我不知道该怎么改变它。 - Bagus Aji Santoso
2个回答

3

您还可以在项目中添加自己的design_bottom_navigation_item.xml文件。因为名称完全相同,所以它将覆盖来自设计库的文件。只需确保ID和视图类型相同即可。

请注意,如果在库的后续版本中更改原始文件的文件名,此修复程序将不起作用。


3

目前还没有一种方法可以更改BottomNavigationMenuView中项目使用的android:maxLines="1"

这是一个变通方法,随着未来版本的发布,它可能会停止工作

BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);

for (int i = 0; i < menuView.getChildCount(); i++) {
      BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
      TextView smallLabel = item.findViewById(R.id.smallLabel);
      TextView largeLabel = item.findViewById(R.id.largeLabel);

      smallLabel.setMaxLines(2);
      largeLabel.setMaxLines(2);
}

简单解释一下。
这是BottomNavigationView使用的布局。在这里,你可以找到:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
  <ImageView
      android:id="@+id/icon"
      ../>
  <com.google.android.material.internal.BaselineLayout
    ..>
    <TextView
        android:id="@+id/smallLabel"
        android:maxLines="1" 
        ../>
    <TextView
        android:id="@+id/largeLabel"
        android:maxLines="1"
        .../>
  </com.google.android.material.internal.BaselineLayout>
</merge>

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