簡體   English   中英

Android 底部導航視圖項標題,未顯示所有文本

[英]Android Bottom Navigation View item title, not shown all text

我想顯示圖標下方的所有文字。請幫忙。 謝謝

它是我的導航菜單 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"/>

使用此方法使所有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)
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM