如何使底部导航显示带有图标和文本的菜单项,除了中心项菜单仅显示图标? [英] How to make bottom navigation show menu items with icon and text except center item menu show only icon?

查看:32
本文介绍了如何使底部导航显示带有图标和文本的菜单项,除了中心项菜单仅显示图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建底部导航栏显示菜单项图标和除中心项之外的文本.例如.我附上了图片

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

推荐答案

试试下面的代码:

XML 文件:

<?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>

助手类:

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

活动中:

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

菜单文件:

<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"/>

这篇关于如何使底部导航显示带有图标和文本的菜单项,除了中心项菜单仅显示图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆