如何禁用BottomNavigationView移位模式? [英] How to disable BottomNavigationView shift mode?
问题描述
BottomNavigationView不会显示不活动的菜单标题。
如何在bottomNavigationBar中显示所有菜单元素的标题?
问题是在我的案例中,仅显示被单击元素的标题。
BottomNavigationView
的实现有条件:当有3个以上的项目时然后使用移位模式。
目前,您无法通过现有的API对其进行更改,并且禁用移位模式的唯一方法是使用反射。
您将需要帮助程序类:
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;
公共类BottomNavigationViewHelper {
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);
//再次设置检查值,因此视图将被更新
// noinspection RestrictedApi
item.setChecked(item.getItemData()。isChecked());
}
} catch(NoSuchFieldException e){
Log.e( BNVHelper,无法获取变速模式字段,e);
} catch(IllegalAccessException e){
Log.e( BNVHelper,无法更改转换模式的值,e);
}
}
}
然后应用<$ c BottomNavigationView
上的$ c> disableShiftMode 方法,但是请记住,如果要从代码中扩展菜单视图,则必须在扩展后执行它。 / p>
示例用法:
BottomNavigationView bottomNavigationView =(BottomNavigationView)findViewById(R.id.bottom_navigation_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
PS。
记住,您每次更改 BottomNavigationView
中的菜单项时,都需要执行此方法。
UPDATE
您还需要更新proguard配置文件(例如proguard-rules.pro),上面的代码使用反射,如果proguard混淆了<$ c,则该代码将不起作用$ c> mShiftingMode 字段。
-keepclassmembers类android.support .design.internal.BottomNavigationMenuView {
boolean mShiftingMode;
}
更新2
正如Jolanda Verhoef指出的那样,新的支持库( 28.0.0-alpha1
)以及新的材料组件库( 1.0.0-beta01
)提供了一个公共属性,该属性可用于操纵3个菜单项上的切换模式。
< com.google.android.material.bottomnavigation.BottomNavigationView
..
app:labelVisibilityMode = labeled
...
/>
在材料组件库中,如果有5个菜单项,则也适用。
更新3
正如@ThomasSunderland指出的那样,您可以将此属性设置为false app:itemHorizontalTranslation = false
不带 Enabled
后缀可禁用移动动画。
您可以查看BottomNavigation样式的完整指南此处
BottomNavigationView doesn't show menu's title that are inactive.
How to show titles of all menu elements in bottomNavigationBar? The problem is that in my case shown only title of element that is clicked.
Implementation of BottomNavigationView
has condition: when there is more than 3 items then use shift mode.
At this moment you cannot change it through existing API and the only way to disable shift mode is to use reflection.
You'll need helper class:
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 {
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);
}
}
}
And then apply disableShiftMode
method on your BottomNavigationView
, but remember if you are inflating menu view from your code, you have to execute it after inflating.
Example usage:
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
PS.
Remember, you'll need to execute this method each time you change menu items in your BottomNavigationView
.
UPDATE
You also need to update proguard configuration file (e.g. proguard-rules.pro), code above uses reflection and won't work if proguard obfuscate the mShiftingMode
field.
-keepclassmembers class android.support.design.internal.BottomNavigationMenuView {
boolean mShiftingMode;
}
Thanks Muhammad Alfaifi for pointing this issue and providing snippet.
UPDATE 2
As Jolanda Verhoef pointed out the new Support library (28.0.0-alpha1
) and also the new Material Components library (1.0.0-beta01
) offers a public property which can be used to manipulate the shifting mode over 3 menu items.
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:labelVisibilityMode="labeled"
...
/>
In Material Components library it also applies if there are 5 menu items.
UPDATE 3
As @ThomasSunderland also pointed out, you can set this property to false app:itemHorizontalTranslation="false"
without Enabled
postfix to disable shifting animation.
you can check the full guide to styling the BottomNavigation here
这篇关于如何禁用BottomNavigationView移位模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!