安卓底部导航栏中超过三个项目

8

我刚接触安卓开发,正在尝试制作一个底部导航栏中包含超过3个元素的应用。我能够显示它们,但它们在结尾处变得过于拥挤,只有三个可以正确显示。以下是我的代码:

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

这里是视图的图片: 这是快照 我被卡住了,请帮忙。

所选项目将始终具有更大的空间(因为它必须显示标题)。当您选择另一个元素时,它会扩展吗? - ianhanniballake
3个回答

20

您可以使用以下方法来避免获取到聚集的菜单项。您需要在onCreate方法中调用此方法,并传递BottomNavigationView。

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

谢谢。我很高兴能帮助到别人。@a.g.thamays - Maulik Dodia

4

我不确定,但据我所知,在底部栏中搭配超过3个项目,会导致对齐失真,这是不可能的。不过,您可以创建一个水平方向的线性布局,并在其中将这些图标设置为图像视图,然后将它们的权重设置为1。

以下是代码:

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

然后还有其他类似的图像视图。


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

完美,它对我有效,但我必须在帮助类中放置@SuppressLint("RestrictedApi")这个注释。这是一个好的做法吗? - Vrajesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接