如何解决BottomNavigation-为什么一个项目的空间与其余项目不同?
我创建了一个BottomNav,其中包含3个常规元素和一个“自定义”点,用于“更多菜单”:
但是我不明白为什么它与其他地方会如此不同?唯一的区别是more菜单不是bottom_nav_graph.xml
的一部分,因为它是自定义添加的项目。除此之外,它与其他项目一样。
编辑:评论中的建议似乎确实有效。但是,仍然没有办法让我均匀分配项目或将它们全部等宽以容纳长名称吗?
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home_screen"
android:icon="@drawable/ic_add_white_24dp"
android:title="@string/title_activity_journey" />
<item
android:id="@+id/navigation_sas"
android:icon="@drawable/ic_check_white_24dp"
android:title="@string/title_activity_sa" />
<item
android:id="@+id/navigation_profile"
android:icon="@drawable/ic_close_white_24dp"
android:title="@string/title_profile" />
<item
android:id="@+id/navigation_more_menu"
android:icon="@drawable/ic_more_horiz_24"
android:title="@string/title_more_menu" />
</menu>
bottom_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/navigation_home_screen">
<fragment
android:id="@+id/navigation_home_screen"
android:name="com.dev.solidmind.ui.HomeScreenFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home_screen" />
<fragment
android:id="@+id/navigation_sas"
android:name="com.dev.solidmind.ui.SA_Fragment"
android:label="@string/title_activity_sa"
tools:layout="@layout/fragment_sa" />
<fragment
android:id="@+id/navigation_profile"
android:name="com.dev.solidmind.ui.ProfileFragment"
android:label="@string/title_profile"
tools:layout="@layout/fragment_profile" />
</navigation>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/bottom_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?android:attr/windowBackground"
app:itemIconTint="@color/bottom_nav_color_selector"
app:itemTextColor="@color/bottom_nav_color_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
private void setUpBottomNavigation() {
BottomNavigationView bottomNavView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home_screen,R.id.navigation_sas,R.id.navigation_profile,R.id.navigation_more_menu)
.build();
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavController navController = Objects.requireNonNull(navHostFragment).getNavController();
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(bottomNavView,navController);
navController.addOnDestinationChangedListener((controller,destination,arguments) -> {
if (destination.getId() == R.id.navigation_home_screen) {
Objects.requireNonNull(getSupportActionBar()).hide();
} else {
Objects.requireNonNull(getSupportActionBar()).show();
}
});
bottomNavView.setonNavigationItemSelectedListener(menuItem -> {
if (menuItem.getItemId() == R.id.navigation_more_menu) {
openBottomSheet();
return true;
} else if (menuItem.getItemId() == R.id.navigation_home_screen) {
navController.navigate(R.id.navigation_home_screen);
return true;
} else if (menuItem.getItemId() == R.id.navigation_sas) {
navController.navigate(R.id.navigation_sas);
return true;
} else if (menuItem.getItemId() == R.id.navigation_profile) {
navController.navigate(R.id.navigation_profile);
return true;
} else return false;
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。