我在使用Android支持设计库25时使用BottomNavigationView.但是当我切换选项卡时,其他选项卡的标题正在隐藏.但实际的底部导航视图没有隐藏问题.但我的藏匿.
但我希望它看起来像那样.有没有想过这样做?我错过了什么?
这是我的代码:
activity_main.xml中
bottom_bar_menu.xml
Danilo Prado.. 93
使用反射的解决方案不再起作用,因为删除了字段mShiftingMode.
现在有一种简单的方法:使用Support Library 28并添加app:labelVisibilityMode="labeled"
到BottomNavigationView
XML声明中.
希望能帮助到你.
使用反射的解决方案不再起作用,因为删除了字段mShiftingMode.
现在有一种简单的方法:使用Support Library 28并添加app:labelVisibilityMode="labeled"
到BottomNavigationView
XML声明中.
希望能帮助到你.
UPDATE
不再需要removeShiftMode(),因为在支持库28.0.0-alpha1中我们现在可以添加标签.
在XML中:
以编程方式更改:
mBottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
为此,请将设计支持库更新为28.0.0-alpha1
这是一个很好的阅读
对于老年人支持图书馆:
在你的.bottom_bar_menu.xml
更改showAsAction
属性
在build.gradle中:
compile 'com.android.support:design:25.3.1'
BOTTOM NAVIGATIONVIEW超过3个项目:使用removeShiftMode()
方法
在BottomNavigationViewHelper.java
使用中:
import android.annotation.SuppressLint; import android.support.design.internal.BottomNavigationItemView; import android.support.design.internal.BottomNavigationMenuView; import android.support.design.widget.BottomNavigationView; import java.lang.reflect.Field; public class BottomNavigationViewHelper { @SuppressLint("RestrictedApi") public static void removeShiftMode(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("BottomNav", "Unable to get shift mode field", e); } catch (IllegalAccessException e) { Log.e("BottomNav", "Unable to change value of shift mode", e); } } }
用它来调用:
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation); BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
它将禁用标题文本的移动动画并启用文本显示.
扫描完BottomNavigationView的源代码后,我找到了
mShiftingMode = mMenu.size() > 3;
在BottomNavigationMenuView.java第265行中,它意味着当菜单大小超过3时,标签标题将被隐藏.因此,如果您想要显示标签标题,您只需要从构建中获取代码并将其更改为以下内容.
mShiftingMode = mMenu.size() > 5;
PS:BottonNavigationView最大标签计数必须介于3和5之间.您可以在BottomNavigationViewNew上获取代码
创建类BottomNavigationViewHelper
import android.annotation.SuppressLint; import android.support.design.internal.BottomNavigationItemView; import android.support.design.internal.BottomNavigationMenuView; import android.support.design.widget.BottomNavigationView; import android.util.Log; import java.lang.reflect.Field; public 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); if(menuView.getChildCount()<6) { 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); } } }
呼叫
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation); BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);