我发现了一个解决方案,将我的进展与
这个 帖子合并。
步骤:
- 更新proguard-rules.pro并同步构建
- 创建帮助程序以禁用BottomNavigationView Shift模式
- 在Menu.xml中创建要隐藏的项目
- 膨胀BottomNavigationView
- 将要隐藏的项目设置为Checked GONE
- 使用Helper来禁用Shifting Mode
输出:
![enter image description here](https://istack.dev59.com/AAfly.webp)
工作代码:
proguard-rules.pro:
-keepclassmembers class android.support.design.internal.BottomNavigationMenuView {
boolean mShiftingMode;
}
BottomNavigationShiftHelper.java:
public class BottomNavigationShiftHelper {
private final static String TAG = "DEBUG_BOTTOM_NAV_UTIL";
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);
item.setShiftingMode(false);
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.d(TAG, "Unable to get shift mode field");
} catch (IllegalAccessException e) {
Log.d(TAG, "Unable to change value of shift mode");
}
}
}
Activity Sample.java:
private void loadNavigationBar() {
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.navigation_bar);
bottomNavigationView.getMenu().findItem(R.id.uncheckedItem).setChecked(true);
bottomNavigationView.findViewById(R.id.uncheckedItem).setVisibility(View.GONE);
BottomNavigationViewUtils.disableShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.newList:
break;
case R.id.loadList:
break;
case R.id.settings:
break;
}
return false;
}
});
}
BottomNavigationMenu.xml:
<?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/newList"
android:enabled="true"
android:icon="@drawable/new_list"
android:title="@string/common.button.list.new"
app:showAsAction="withText" />
<item
android:id="@+id/loadList"
android:enabled="true"
android:icon="@drawable/load"
android:title="@string/common.button.list.load"
app:showAsAction="withText" />
<item
android:id="@+id/settings"
android:enabled="true"
android:icon="@drawable/settings"
android:title="@string/common.label.settings"
app:showAsAction="withText" />
<item
android:id="@+id/uncheckedItem"
android:title="" />
</menu>
BottomNavigationComponent(在Activity.xml中):
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
android:background="@drawable/BottomNavigationMenu.xml"
app:menu="@menu/supercart_bottom_navigation" />
BottomNavigationView
添加一个属性。请看我的回答。 - Dan Bray