我正在尝试将 BottomNavigationView 与导航编辑器一起使用。我已经按照给定的方式实现了所有内容,但只显示主页,当我更改选项卡时,它不会更改片段。
这是主要活动代码:
public class MainActivity extends AppCompatActivity {
BottomNavigationView mainBottomNavigation;
NavController navController;
NavHostFragment navHostFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBottomNavigation = findViewById(R.id.bottom_navigation);
navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavigationUI.setupWithNavController(mainBottomNavigation, Navigation.findNavController(this,R.id.nav_host_fragment));
} }
这是主要的活动 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是菜单文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_location"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_location" />
<item
android:id="@+id/navigation_sightseeing"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_sightseeing" />
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_gallery"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_gallery" />
<item
android:id="@+id/navigation_contact_us"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_contact_us" />
</menu>
这是我创建的 nav_graph :
<?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/nav_graph"
app:startDestination="@id/navigation_home">
<fragment
android:id="@+id/navigation_location"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_location"
tools:layout="@layout/fragment_location" />
<fragment
android:id="@+id/navigation_sightseeing"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_sightseeing"
tools:layout="@layout/fragment_sight_seeing" />
<fragment
android:id="@+id/navigation_home"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_gallery"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/navigation_contact_us"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_contact_us"
tools:layout="@layout/fragment_contact_us" />
</navigation>