在底部导航栏超过3项android
我是新来的android和我想在底部导航栏中使用超过3个元素的应用程序。我能够显示它们,但它们在最后聚集,只有三个正确可见。这里是我的代码:在底部导航栏超过3项android
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="15dp"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_items" />
这里是视角的图像: This is the snapshot
我坚持请帮助..
我不知道,但是据我所知,这是不可以使用底部条带陪同超过3个物品,而不会扭曲对齐。 你能反正要做的就是与水平方向的线布局,并且在设置这些图标作为图像的意见,然后作出自己的体重为1
下面是代码,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#fff">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="(YOUR IMAGE SOURCE)"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"/>
然后像这样的其他图像视图。
您可以使用下面的方法来获取群集菜单项。您必须在传递BottomNavigationView的onCreate方法中调用此方法。
// Method for disabling ShiftMode of BottomNavigationView
private 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.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
它工作正常 –
谢谢。我很高兴我帮了别人.. @ a.g.thamays –
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
navigation.xml(inside menu)
<?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/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home"
app:showAsAction="always|withText"
android:enabled="true"/>
inside oncreate method
BottomNavigationView navigation = (BottomNavigationView)findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation);//Dont forgot this line
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);
}
}
}
完美,它适用于我,但我必须把@SuppressLint(“RestrictedApi”)这个注解放在一个帮助类中。这是不错的做法吗? – Vrajesh
所选项目将始终具有的空间较大的量(假定它有显示标题)。当你选择另一个元素时,它会展开吗? – ianhanniballake