带底部栏的浮动操作按钮

时间:2018-07-13 05:58:50

标签: java android android-layout android-xml

我发现了一种通过在菜单中创建一个空项目来添加FAB的方法,但是该方法仅在有3个项目时才有效,对于3个以上的项目,项目之间的空间将是不均匀的。

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_anchor="@id/bottom_bar"
    android:id="@+id/frame"
    />

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:id="@+id/bottom_bar"
    android:elevation="20dp"
    android:layout_gravity="bottom|start"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/colorAccent"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/menu">

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

<android.support.design.widget.FloatingActionButton
    android:layout_width="52dp"
    android:layout_height="52dp"
    android:layout_gravity="bottom|center_horizontal"
    android:layout_marginBottom="25dp"
    android:elevation="30dp"
    android:src="@drawable/ic_add_black_24dp"
    app:elevation="10dp" />

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

这是我的菜单

菜单文件

<?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/trending"
    android:icon="@drawable/ic_trending"
    android:title="Trending" />
<item
    android:id="@+id/dashboard"
    android:icon="@drawable/ic_dashboard"
    android:title="Dashboard" />
<item android:title="   " />
<item
    android:id="@+id/people"
    android:icon="@drawable/ic_people"
    android:title="People" />
<item
    android:id="@+id/account"
    android:icon="@drawable/ic_account"
    android:title="Account" />
</menu>

Screenshot of how the bottom bar looks now

1 个答案:

答案 0 :(得分:0)

从另一个现成的SO问题中得到了答案... android的底部导航视图中有一个默认设置,当项目的数量大于3时,该项目会进行移动。 因此,要消除这种转变,我们必须使用一个帮助器类。

class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
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);
    }
}
}

将这些行添加到progaurd-rules.pro中,因为新的设计支持库阻止了此帮助器类的正常工作(不会损害代码)

-keepclassmembers class android.support.design.internal.BottomNavigationMenuView {
boolean mShiftingMode;
}
-keepclassmembers class android.support.design.internal.BottomNavigationItemView {
int mShiftAmount;
}