我有一个包含很长标签文本的 BottomNavigationView
,但是它没有显示完整的标签。
我发现 BottomNavigationView
的标签使用了 maxLines="1"
属性,但我不知道如何将其修改为允许两行标签。
我有一个包含很长标签文本的 BottomNavigationView
,但是它没有显示完整的标签。
我发现 BottomNavigationView
的标签使用了 maxLines="1"
属性,但我不知道如何将其修改为允许两行标签。
您还可以在项目中添加自己的design_bottom_navigation_item.xml
文件。因为名称完全相同,所以它将覆盖来自设计库的文件。只需确保ID和视图类型相同即可。
请注意,如果在库的后续版本中更改原始文件的文件名,此修复程序将不起作用。
目前还没有一种方法可以更改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>