简体   繁体   English

底部导航栏android中超过3项

[英]More than 3 items in bottom navigation bar android

I am new to android and I am trying to make an app with more than 3 elements in the bottom navigation bar. 我是Android的新手,我正在尝试在底部导航栏中创建一个包含3个以上元素的应用程序。 I am able to display them but they are getting clustered at the end and only three are visible properly. 我能够显示它们,但它们最终聚集在一起,只有三个可以正确显示。 Here is my code: 这是我的代码:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="15dp"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_nav_items" />

Here is the image of the view: This is the snapshot 以下是视图的图像: 这是快照

I am stuck please help.. 我卡住了请帮忙..

You can use below method for not getting clustered menu items. 您可以使用以下方法来获取群集菜单项。 You have to call this method in onCreate method passing BottomNavigationView. 您必须在传递BottomNavigationView的onCreate方法中调用此方法。

// Method for disabling ShiftMode of BottomNavigationView
private 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);
            item.setShiftingMode(false);
            // set once again checked value, so view will be updated
            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);
    }
}

I am not sure, but to my knowledge, it is not possible to accompany more than 3 items using the bottom bar, without distorting the alignment. 我不确定,但据我所知,使用底栏不可能伴随超过3个项目,而不会扭曲对齐方式。 What you can do anyways is make a linear layout with horizontal orientation, and in that set those icons as image views and then make their weight as 1. 无论如何你可以做的是做一个水平方向的线性布局,然后将这些图标设置为图像视图,然后将它们的权重设为1。

Here is the code, 这是代码,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:layout_alignParentBottom="true"
    android:background="#fff">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="(YOUR IMAGE SOURCE)"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"/>

And then other image views like this. 然后像这样的其他图像视图。

     <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/navigation" />


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

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

    inside oncreate method
     BottomNavigationView navigation = (BottomNavigationView)findViewById(R.id.navigation);
      BottomNavigationViewHelper.disableShiftMode(navigation);//Dont forgot this line




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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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