简体   繁体   中英

Android Bottom Navigation View item title, not shown all text

I want to show all of the text below the icon.Please help. Thanks

its my navigation menu 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"/>

Use this method to make all the BottomNavigationView 's labels to show 2 lines:

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);
                }
            }
        }
    }
}

and call it by:

fixBottomNavigationText(bottomNavigationView);

change bottomNavigationView to the id of your BottomNavigationView .
It is in Java and if you have trouble writing it in Kotlin let me know.
Edit In 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)
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM