如何强制 BottomNavigationView 标签为多行
How to force BottomNavigationView label to multiline
我有一个 BottomNavigationView
标签文本很长,但没有显示完整的标签。
我发现 BottomNavigationView
上的标签有一个 maxLines="1"
,但我不知道如何修改它以允许 2 行标签。
目前没有方法来更改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
使用的 layout。在这里你可以找到:
<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>
您也可以在项目中添加自己的 design_bottom_navigation_item.xml
。由于名称完全相同,它将覆盖设计库中的名称。只要确保 ID 和视图类型相同即可。
请注意,如果他们在以后的库版本中更改了原始文件名,修复将不再有效。
我有一个 BottomNavigationView
标签文本很长,但没有显示完整的标签。
我发现 BottomNavigationView
上的标签有一个 maxLines="1"
,但我不知道如何修改它以允许 2 行标签。
目前没有方法来更改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
使用的 layout。在这里你可以找到:
<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>
您也可以在项目中添加自己的 design_bottom_navigation_item.xml
。由于名称完全相同,它将覆盖设计库中的名称。只要确保 ID 和视图类型相同即可。
请注意,如果他们在以后的库版本中更改了原始文件名,修复将不再有效。