Android底部导航查看项目标题,未显示所有文本

时间:2018-11-01 11:44:09

标签: android kotlin

我想在图标下方显示所有文本。请提供帮助。谢谢

其导航菜单xml;

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/navigation_credit_card"
    android:icon="@drawable/ic_opened_credit_card_48px"
    android:title="@string/credit_or_bank_cart_payment"
    app:showAsAction="ifRoom"/>

<item
    android:id="@+id/navigation_bank_transfer"
    android:icon="@drawable/ic_opened_bank_transfer_48px"
    android:title="@string/bank_transfer_payment"
    app:showAsAction="ifRoom"/>

<item
    android:id="@+id/navigation_cod"
    android:icon="@drawable/ic_opened_cash_on_delivery_48px"
    android:title="@string/cash_on_delivery"
    app:showAsAction="ifRoom"/>

1 个答案:

答案 0 :(得分:1)

使用此方法使所有BottomNavigationView的标签显示2行:

private void fixBottomNavigationText(BottomNavigationView bottomNavigationView) {
    for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {
        View item = bottomNavigationView.getChildAt(i);

        if (item instanceof BottomNavigationMenuView) {
            BottomNavigationMenuView menu = (BottomNavigationMenuView) item;

            for (int j = 0; j < menu.getChildCount(); j++) {
                View menuItem = menu.getChildAt(j);

                View small = menuItem.findViewById(android.support.design.R.id.smallLabel);
                if (small instanceof TextView) {
                    ((TextView) small).setLines(2);
                }
                View large = menuItem.findViewById(android.support.design.R.id.largeLabel);
                if (large instanceof TextView) {
                    ((TextView) large).setLines(2);
                }
            }
        }
    }
}

并通过以下方式致电:

fixBottomNavigationText(bottomNavigationView);

bottomNavigationView更改为BottomNavigationView的ID。
它是用Java编写的,如果您用Kotlin编写时遇到麻烦,请告诉我。
编辑在Kotlin中:

fun fixBottomNavigationText(bottomNavigationView: BottomNavigationView) {
    for (i in 0 until bottomNavigationView.getChildCount()) {
        val item = bottomNavigationView.getChildAt(i)

        if (item is BottomNavigationMenuView) {
            val menu = item as BottomNavigationMenuView

            for (j in 0 until menu.getChildCount()) {
                val menuItem = menu.getChildAt(j)

                val small: View = menuItem.findViewById(android.support.design.R.id.smallLabel)
                if (small is TextView) {
                    (small as TextView).setLines(2)
                }
                val large: View = menuItem.findViewById(android.support.design.R.id.largeLabel)
                if (large is TextView) {
                    (large as TextView).setLines(2)
                }
            }
        }
    }
}