我的导航栏未在 Android Studio Kotlin 中的片段之间切换
My Navigation bar is not switching between fragments in Android Studio Kotlin
- activity
class ChangeInfoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_change_info)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConf = AppBarConfiguration(setOf(
R.id.navigation_person, R.id.navigation_chat
))
setupActionBarWithNavController(navController, appBarConf)
navView.setupWithNavController(navController)
}
}
- 布局
<?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:layout_width="match_parent"
android:background="@drawable/backgroundsecond"
android:layout_height="match_parent"
tools:context=".ChangeInfoActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/nav_shape"
app:itemIconTint="@color/red"
app:itemTextColor="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_review" />
<fragment
android:id="@+id/nav_host_fragment"
app:navGraph="@navigation/nav_graph"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 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/personFragment">
<fragment
android:id="@+id/personFragment"
android:name="com.example.autentification.fragments.PersonFragment"
android:label="fragment_person"
tools:layout="@layout/fragment_person" >
<action
android:id="@+id/action_personFragment_to_chatFragment"
app:destination="@id/chatFragment" />
</fragment>
<fragment
android:id="@+id/chatFragment"
android:name="com.example.autentification.fragments.ChatFragment"
android:label="fragment_chat"
tools:layout="@layout/fragment_chat" >
<argument
android:name="nameofuser"
app:argType="string"
android:defaultValue="unnamed warrior" />
</fragment>
</navigation>
- 菜单
<?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_person"
android:icon="@drawable/ic_baseline_person_24"
android:title="Person" />
<item
android:id="@+id/navigation_chat"
android:icon="@drawable/ic_baseline_message_24"
android:title="Chat"/>
</menu>
看来我已经做好了一切,但我仍然无法在 2 个片段之间切换。
(P.S第一次在这个平台上提问,不好意思)
您需要将 navGraph
中的片段 ID 更改为与菜单的 ID 相同
将其应用于 navGraph
:
<?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_person">
<fragment
android:id="@+id/navigation_person"
android:name="com.example.autentification.fragments.PersonFragment"
android:label="fragment_person"
tools:layout="@layout/fragment_person" >
<action
android:id="@+id/action_personFragment_to_chatFragment"
app:destination="@id/chatFragment" />
</fragment>
<fragment
android:id="@+id/navigation_chat"
android:name="com.example.autentification.fragments.ChatFragment"
android:label="fragment_chat"
tools:layout="@layout/fragment_chat" >
<argument
android:name="nameofuser"
app:argType="string"
android:defaultValue="unnamed warrior" />
</fragment>
</navigation>
- activity
class ChangeInfoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_change_info)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConf = AppBarConfiguration(setOf(
R.id.navigation_person, R.id.navigation_chat
))
setupActionBarWithNavController(navController, appBarConf)
navView.setupWithNavController(navController)
}
}
- 布局
<?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:layout_width="match_parent"
android:background="@drawable/backgroundsecond"
android:layout_height="match_parent"
tools:context=".ChangeInfoActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/nav_shape"
app:itemIconTint="@color/red"
app:itemTextColor="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_review" />
<fragment
android:id="@+id/nav_host_fragment"
app:navGraph="@navigation/nav_graph"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 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/personFragment">
<fragment
android:id="@+id/personFragment"
android:name="com.example.autentification.fragments.PersonFragment"
android:label="fragment_person"
tools:layout="@layout/fragment_person" >
<action
android:id="@+id/action_personFragment_to_chatFragment"
app:destination="@id/chatFragment" />
</fragment>
<fragment
android:id="@+id/chatFragment"
android:name="com.example.autentification.fragments.ChatFragment"
android:label="fragment_chat"
tools:layout="@layout/fragment_chat" >
<argument
android:name="nameofuser"
app:argType="string"
android:defaultValue="unnamed warrior" />
</fragment>
</navigation>
- 菜单
<?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_person"
android:icon="@drawable/ic_baseline_person_24"
android:title="Person" />
<item
android:id="@+id/navigation_chat"
android:icon="@drawable/ic_baseline_message_24"
android:title="Chat"/>
</menu>
看来我已经做好了一切,但我仍然无法在 2 个片段之间切换。 (P.S第一次在这个平台上提问,不好意思)
您需要将 navGraph
中的片段 ID 更改为与菜单的 ID 相同
将其应用于 navGraph
:
<?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_person">
<fragment
android:id="@+id/navigation_person"
android:name="com.example.autentification.fragments.PersonFragment"
android:label="fragment_person"
tools:layout="@layout/fragment_person" >
<action
android:id="@+id/action_personFragment_to_chatFragment"
app:destination="@id/chatFragment" />
</fragment>
<fragment
android:id="@+id/navigation_chat"
android:name="com.example.autentification.fragments.ChatFragment"
android:label="fragment_chat"
tools:layout="@layout/fragment_chat" >
<argument
android:name="nameofuser"
app:argType="string"
android:defaultValue="unnamed warrior" />
</fragment>
</navigation>