问题描述
当我喜欢BottomNavigationView
中的设计时,我决定使用它为我的App实现一个新菜单,而不仅仅是使用简单的按钮.
As I liked the design from BottomNavigationView
I decided to implement a new Menu for my App with it, instead of just using simple buttons.
我以这篇帖子为指南.
根据BottomNavigationView
的文档 ,其目的是
就我而言,我只希望每个MenuItem
都启动一个活动,但是默认情况下始终选择一个MenuItem
:
In my case, I just want each MenuItem
to launch an activity, but by default there is always one MenuItem
selected:
我尝试通过以下方式将颜色设置为白色:
I tried to set the color to white with:
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
仍然可见,选择的MenuItem
与其他(标题大小较大)不同,这仍然困扰着我:
Still, visibly selected MenuItem
is different from others (Title size bigger), which is still bothering me:
我想到了放置一个隐藏的MenuItem
以便进行选择的方法:
I came with the idea to place a hidden MenuItem
to select like:
<item
android:id="@+id/uncheckedItem"
android:title="" />
并使其显示为GONE
:
bottomNavigationView.getMenu().findItem(R.id.uncheckedItem).setChecked(true);
bottomNavigationView.findViewById(R.id.uncheckedItem).setVisibility(View.GONE);
这将使所有MenuItems都未选中,但是默认情况下BottomNavigationView
隐藏标题,因为即使第四个MenuItem
设置为GONE
,它也要显示3个以上的MenuItems:
This makes all MenuItems unchecked, but by default BottomNavigationView
is hidding Titles, as it has more than 3 MenuItems to display, even if the fourth MenuItem
is settle to GONE
:
所以我的问题仍然存在,是否存在取消/取消选择所有MenuItems并使标题保持显示的行为?
So my question remains, is there away/hack to unselect all MenuItems and keep its titles being displayed?
推荐答案
我找到了自己的解决方案,将我的进步与帖子.
I found my own solution merging my progress with this post.
步骤:
- 更新proguard-rules.pro并同步构建
- 创建帮助器以禁用BottomNavigationView Shift模式
- 创建要隐藏在Menu.xml上的项目
- 膨胀BottomNavigationView
- 将商品"设置为已检查的已消失"
- 使用帮助器禁用换档模式
输出:
工作代码:
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);
// set once again checked value, so view will be updated
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");
}
}
}
活动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:
//Do The Math
break;
case R.id.loadList:
//Do The Math
break;
case R.id.settings:
//Do The Math
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-如何取消选中所有MenuItems并保持标题显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!