BottomNavigationViewの使い方まとめ。
・参考動画
https://www.youtube.com/watch?v=JjfSjMs0ImQ
・公式ドキュメント
・MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 以下BottomNavigationViewの設定
val navVie: BottomNavigationView = findViewById(R.id.bottom_navigation)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_first_fragment, R.id.navigation_second_fragment, R.id.navigation_third_fragment))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
・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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu_bottom_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="@id/nav_host_fragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
※注意点
<BottomNavigationView />
↓
<fragment />
の順にタグを書く
・nav_greph.xml(res/navigation/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/nav_graph"
app:startDestination="@id/navigation_first_fragment">
<fragment
android:id="@+id/navigation_first_fragment"
android:name="com.example.myapplicatoin.FirstFragment"
android:label="first fragment"
tools:layout="@layout/fragment_first" />
<fragment
android:id="@+id/navigation_second_fragment"
android:name="com.example.myapplication.SecondFragment"
android:label="second fragment"
tools:layout="@layout/fragment_second" />
<fragment
android:id="@+id/navigation_third_fragment"
android:name="com.example.myapplication.ThridFragment"
android:label="thrid fragment"
tools:layout="@layout/fragment_third" />
</navigation>
※注意点
<fragment>タグのidとbottom_menu.xmlで作成した<item>タグのを合わせる。
・bottom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_first_fragment"
android:icon="@drawable/ic_icon1"
android:title="first fragment" />
<item
android:id="@+id/navigation_second_fragment"
android:icon="@drawable/ic_icon2"
android:title="second_fragment" />
<item
android:id="@+id/navigation_third_fragment"
android:icon="@drawable/ic_icon3"
android:title="third fragment" />
</menu>
※注意点
上記にも書いたが、<fragment>タグのidと<item>タグのを合わせる。
合わせることで、MainActivity.ktの設定で、画面遷移を行うことが出来る。