Jetpack Navigation 与 BottomNavigation 使用

使用Navigation 需要添加的依赖

    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'

 

main_activity.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"
    android:fitsSystemWindows="true"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        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" />

    <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_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

menu 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home_navigatio_icon_selector"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_device"
        android:icon="@drawable/bind_navigatio_icon_selector"
        android:title="@string/title_device" />
    <item
        android:id="@+id/navigation_trval"
        android:icon="@drawable/measure_navigatio_icon_selector"
        android:title="@string/title_travel" />

    <item
        android:id="@+id/navigation_user"
        android:icon="@drawable/found_navigatio_icon_selector"
        android:title="@string/title_user_center" />

</menu>

 

res 目录下创建 navigation 文件夹,然后创建 main_navigation.xml 文件

点击按钮可以进入对应的Fragment 视图, 选中对应的fragment  可以快速创建 action . 

<?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/main_navigation"
    app:startDestination="@id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.xy.bhandsh.home.ui.HomeFragment"
        android:label="HomeFragment"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/navigation_device"
        android:name="com.xy.bhandsh.device.ui.DeviceFragment"
        android:label="device_fragment"
        tools:layout="@layout/device_fragment" />
    <fragment
        android:id="@+id/navigation_trval"
        android:name="com.xy.bhandsh.trval.ui.TrvalFragment"
        android:label="trval_fragment"
        tools:layout="@layout/trval_fragment" />

    <fragment
        android:id="@+id/navigation_user"
        android:name="com.xy.bhandsh.user.ui.UserCenterFragment"
        android:label="user_center_fragment"
        tools:layout="@layout/user_center_fragment" />
</navigation>

其中 startDestination = @id/navigation_home 指定 navigation 的起始位置. 

fragment 对应id 应与 menu 中的id 一一对应

MainActivity 关键代码

  val navView: BottomNavigationView = findViewById(R.id.nav_view)
        navView.labelVisibilityMode = LABEL_VISIBILITY_LABELED //label 大于3时 仍显示文字
        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值