底部菜单不起作用仅显示活动MenuItem

时间:2018-03-15 17:37:56

标签: java android xml material-design bottomnavigationview

我在底部菜单中添加了五个项目,只有第一个是活动的,其他图标是可见的,但是它们下方的文本是隐藏的,当我试图点击这些项目时,没有任何事情会发生。它可以在底部菜单中使用三个项目。

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home" />

<item
    android:id="@+id/navigation_browse"
    android:icon="@drawable/ic_work_black_24dp"
    android:title="@string/title_project" />
<item
    android:id="@+id/navigation_order"
    android:icon="@drawable/ic_shopping_cart_black_24dp"
    android:title="@string/title_oredr" />
<item
    android:id="@+id/navigation_partner"
    android:icon="@drawable/ic_people_black_24dp"
    android:title="@string/title_partners" />
<item
    android:id="@+id/navigation_contact"
    android:icon="@drawable/ic_email_black_24dp"
    android:title="@string/title_contact" />
</menu>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mobdev.micronets.MainActivity">

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/nav_item_colors"
    app:itemTextColor="@color/nav_item_colors"
    app:menu="@menu/navigation" />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

将最后一行代码添加到navigation.xml中:

<item
    android:id="@+id/navigation_home"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home"    
    app:showAsAction="always|withText"/>

因此,您的项目将始终与文本一起显示。 但是,有时它不合适,因此您需要删除筛选模式以适应。

创建这个java类。

<强> BottomNavigationHelper.java

public class BottomNavigationHelper {

    @SuppressLint("RestrictedApi")
    public static void removeShiftMode(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("BottomNav", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BottomNav", "Unable to change value of shift mode", e);
        }
    }

}

然后在您拥有BottomNavigationView的所有活动中,您可以在onCreate中调用此方法。

<强> MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottomNavView);
        setupBottomNavigationView();

    }

private void setupBottomNavigationView(){

        BottomNavigationHelper.removeShiftMode(bottomNavigationView);

    }