android实现项目开发二--- Navigation 实现底部导航栏的功能

接着第一天的项目,继续往前推进。

1. 底部导航栏的设置

2. 引入ARouter组件。

底部导航栏功能实现

1. res下创建menu,新建xml文件内容如下

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="@string/menu_merch_first" />
    <item
        android:id="@+id/navigation_order"
        android:icon="@drawable/ic_order"
        android:title="@string/menu_merch_second" />
    <item
        android:id="@+id/navigation_user"
        android:icon="@drawable/ic_user"
        android:title="@string/menu_merch_third" />
</menu>

2. res下创建navigation,新建文件内容如下

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

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.duoduomi.app.page.merch.HomeFragment"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_order"
        android:name="com.duoduomi.app.page.merch.OrderFragment"
        tools:layout="@layout/fragment_order" />

    <fragment
        android:id="@+id/navigation_user"
        android:name="com.duoduomi.app.page.merch.UserFragment"
        tools:layout="@layout/fragment_user" />

</navigation>

3. 布局文件XML中设置如下

<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"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="?android:attr/windowBackground"
        app:labelVisibilityMode="labeled"
        app:itemIconTint="@drawable/bottom_nav_color_selector"
        app:itemTextColor="@drawable/bottom_nav_color_selector"
        app:itemRippleColor="@null"
        app:itemBackground="@null"
        app:menu="@menu/bottom_navigation_menu" />

    <!-- 页面中显式fragment的容器-->
    <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/bottom_navigation"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

4. activity文件设置

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

        NavController controller = Navigation.findNavController(this, R.id.nav_host_fragment);
        //在选择菜单时调用,当目的地更改时,BottomNavigationView中的选定项将自动更新
        NavigationUI.setupWithNavController(bottomNavigationView, controller);

5.  效果如下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值