[英]floating action button with bottom bar
我發現了一種通過在菜單中創建一個空項目來添加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>
得到了另一個開箱即用的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.