【07】Kotlin项目实操之Navigation使用

(1)一个人只要自己不放弃自己,整个世界也不会放弃你.
(2)天生我才必有大用
(3)不能忍受学习之苦就一定要忍受生活之苦,这是多么痛苦而深刻的领悟.
(4)做难事必有所得
(5)精神乃真正的刀锋
(6)战胜对手有两次,第一次在内心中.
(7)好好活就是做有意义的事情.
(8)亡羊补牢,为时未晚
(9)科技领域,没有捷径与投机取巧。
(10)有实力,一年365天都是应聘的旺季,没实力,天天都是应聘的淡季。
(11)基础不牢,地动天摇
(12)写博客初心:成长自己,辅助他人。当某一天离开人世,希望博客中的思想还能帮人指引方向.
(13)编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

【07】Kotlin项目实操之Navigation使用

1.布局

1.1Activity布局

<?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">

    <!-- 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/bottom_nav_menu"
        tools:ignore="MissingClass" />

    <!-- 首页的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/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

1.2菜单布局

<?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/ic_home_black_24dp"
        android:title="@string/title_home" />

    <!-- 收藏  ...... 可能会变 -->
    <item
        android:id="@+id/navigation_collect"
        android:icon="@drawable/ic_collect_black_24dp"
        android:title="@string/title_collect" />

    <!-- 个人 -->
    <item
        android:id="@+id/navigation_personal"
        android:icon="@drawable/ic_personal_black_24dp"
        android:title="@string/title_personal" />
</menu>

1.3菜单页面布局

<?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/mobile_navigation"
    app:startDestination="@+id/navigation_home"
    tools:ignore="UnusedNavigation">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.gdc.kotlinproject.modules.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_collect"
        android:name="com.gdc.kotlinproject.modules.collect.CollectFragment"
        android:label="@string/title_collect"
        tools:layout="@layout/fragment_collect" />

    <fragment
        android:id="@+id/navigation_personal"
        android:name="com.gdc.kotlinproject.modules.personal.PersonalFragment"
        android:label="@string/title_personal"
        tools:layout="@layout/fragment_personal" />

</navigation>

2.Activity加载

/**
 * @author XiongJie
 * @version appVer
 * @Package com.gdc.kotlinproject.home
 * @file
 * @Description:主页
 * @date 2021-6-4 17:58
 * @since appVer
 */
class MainActivity:AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //1.总控制器 底部导航菜单
        val navView : BottomNavigationView = findViewById(R.id.nav_view)

        //2.导航按钮
        val appBarConfiguration : AppBarConfiguration = AppBarConfiguration.Builder(
            R.id.navigation_home,
            R.id.navigation_collect,
            R.id.navigation_personal
        ).build()

        //3.导航控制器页面展示区域
        val navController : NavController = Navigation.findNavController(this,R.id.nav_host_fragment)

        //4.初始化导航控制器UI
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration)

        //5.初始化总控制器UI
        NavigationUI.setupWithNavController(navView,navController)

    }

}

3.打赏鼓励

感谢您的细心阅读,您的鼓励是我写作的不竭动力!!!

3.1微信打赏

在这里插入图片描述

3.2支付宝打赏

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值