How to make bottom navigation show menu items with icon and text except center item menu show only icon? [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-17 10:33:18

问题


I want to create bottom navigation bar show menu items icon and text except center item. eg. i have attached image


回答1:


Try below code:

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/gray"
        android:foregroundGravity="top"
        android:gravity="top">

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">


        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </FrameLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomnav"
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:layout_weight="1"
            android:background="null"
            app:itemIconTint="@color/green"
            app:itemTextColor="@color/green"
            app:menu="@menu/main">


        </android.support.design.widget.BottomNavigationView>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:layout_weight="0.5"
            android:src="@drawable/camera" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomnav1"
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:layout_weight="1"
            android:background="null"
            app:itemIconTint="@color/green"
            app:itemTextColor="@color/green"
            app:menu="@menu/main">


        </android.support.design.widget.BottomNavigationView>
    </LinearLayout>
</LinearLayout>

Helper class:

public class BottomNavigationViewHelper {
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}

In activity :

BottomNavigationView bnav = (BottomNavigationView) findViewById(R.id.bottomnav);
BottomNavigationViewHelper.disableShiftMode(bnav);

Menu file :

<item
    android:id="@+id/place"
    android:icon="@drawable/ic_place_black_24dp"
    android:title="Place"/>

<item
    android:id="@+id/home"
    android:icon="@drawable/ic_account_balance_black_24dp"
    android:title="Home"/>



来源:https://stackoverflow.com/questions/44342530/how-to-make-bottom-navigation-show-menu-items-with-icon-and-text-except-center-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!