Android BottomNavigationView+Novigation でボトムナビゲーション機能を実現

効果を達成するここに画像の説明を挿入

1.nav_graph.xml を作成する

res / NEW / Android Resource File を右クリックして、Navigation タイプの 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">
</navigation>
2.対応する4つのフラグメントを作成します

作成した 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/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.moyihen.smartoa.ui.home.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/recordFragment"
        android:name="com.moyihen.smartoa.RecordFragment"
        android:label="fragment_record"
        tools:layout="@layout/fragment_record" />
    <fragment
        android:id="@+id/clockFragment"
        android:name="com.moyihen.smartoa.ClockFragment"
        android:label="fragment_clock"
        tools:layout="@layout/fragment_clock" />
    <fragment
        android:id="@+id/myFragment"
        android:name="com.moyihen.smartoa.MyFragment"
        android:label="fragment_my"
        tools:layout="@layout/fragment_my" />
</navigation>
3.BottomNavigationView に必要な navigation_menu.xml を作成します。

res / NEW / Android Resource File を右クリックして、Menu タイプの navigation_menu.xml ファイルを作成します。

注: ここでのアイテム ID は、上記の nav_graph.xml フラグメントの ID と一致している必要があります。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/homeFragment"
        android:icon="@drawable/nav_btn_1"
        android:title="首页" />
    <item
        android:id="@+id/recordFragment"
        android:icon="@drawable/nav_btn_2"
        android:title="记录" />
    <item
        android:id="@+id/clockFragment"
        android:icon="@drawable/nav_btn_3"
        android:title="定时" />
    <item
        android:id="@+id/myFragment"
        android:icon="@drawable/nav_btn_3"
        android:title="我的" />
</menu>
4. MainActivity を作成する

Activity_main.xml は BottomNavigationView とフラグメントを配置します
。注: 下部のナビゲーション ボタンが 3 つ以上ある場合、テキストは表示されず、app:labelVisibilityMode="labeled" を追加する必要があります。

<?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:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/navigation_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
    
    

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        val navView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.homeFragment,
                R.id.recordFragment,
                R.id.clockFragment,
                R.id.myFragment
            )
        )
        //App bar与NavController绑定 动态改变title
        setupActionBarWithNavController(navController,appBarConfiguration)

        navView.setupWithNavController(navController)

    }
}

終わり

おすすめ

転載: blog.csdn.net/qq_35193677/article/details/124750384