Jetpack架构组件_Navigaiton组件_3.NavigationUI 1分钟实现首页框

1.Android底部导航栏之官方BottomNavigationView简介

 ​​​        “Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation.”应用底部的导航栏,是material design bottom navigation的实现.

        “Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. It should be used when application has three to five top-level destinations.”
底部导航使用户更方便的查看和切换最高层级的导航界面,适用于有三到五个Tab的情况(推荐是三到五个,太多和太少都不应该用)

2.示例项目

2.1新建工程

        点击菜单File>New>New Project>Bottom Navigation Views Activity,创建一个新项目。

        填写项目名称bottomNavMain、语言选择Java,Minimum SDK我这里选择API27。

2.2MainActivity 

        MainActivity.java代码如下:

package com.gaoting.bottomnavmain;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.gaoting.bottomnavmain.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

}

2.3activity_main布局 

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

    <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: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>

2.4Mobile_navigation导航 

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

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.gaoting.bottomnavmain.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.gaoting.bottomnavmain.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.gaoting.bottomnavmain.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

2.5Menu菜单

<?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_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

2.6运行效果

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《Jetpack架构组件从入门到精通》.pdf 是一本介绍Android Jetpack架构组件的书籍。Jetpack是Google官方提供的一套组件库,旨在帮助开发者更轻松地构建高质量的Android应用程序。 这本书从入门到精通地介绍了Jetpack架构组件的各个方面。首先,它详细解释了Jetpack的基本概念和使用方法。读者可以学习到Jetpack的核心组件,如ViewModel、LiveData、Room和Navigation等,以及它们在构建Android应用时的作用。 此外,这本书还介绍了Jetpack架构组件的一些高级技术和最佳实践。读者可以学习到如何使用WorkManager实现后台任务,如何使用DataBinding进行数据绑定,以及如何使用Paging构建分页列表等。 除了介绍Jetpack架构组件的基本用法和高级技术,这本书还提供了一些实际应用示例和案例分析。读者可以通过参考这些示例来更好地理解和应用Jetpack架构组件。 总之,《Jetpack架构组件从入门到精通》.pdf 是一本全面而深入地介绍Jetpack架构组件的书籍。无论是初学者还是有一定经验的开发者,都可以通过阅读这本书来提升自己在Android应用开发中使用Jetpack的能力。 ### 回答2: 《Jetpack架构组件从入门到精通》.pdf 是一本关于Jetpack架构组件的学习指南。JetpackAndroid开发中一套强大而且灵活的组件集合,旨在帮助开发者更轻松地构建高质量的Android应用程序。 该PDF文件从入门到精通地介绍了Jetpack架构组件的各个方面。首先,它向读者介绍了Jetpack的概念和使用场景,以帮助读者了解为什么应该学习和使用这些组件。 接下来,该指南逐一介绍了Jetpack架构组件的不同模块,包括ViewModel、LiveData、Room、Navigation等。它详细解释了每个组件的功能和用法,并通过实际示例演示了如何在项目中使用它们。 此外,该指南还提供了一些常见的最佳实践和实用技巧,以帮助读者更好地理解和运用Jetpack架构组件。它还包含了一些常见问题和解决方案,帮助读者避免在实践中遇到的常见问题。 最后,该指南还提供了一些参考资料和进一步学习资源,以帮助读者深入学习和掌握Jetpack架构组件。 总的来说,该《Jetpack架构组件从入门到精通》.pdf提供了一个全面而详尽的学习指南,帮助读者了解和应用Jetpack架构组件,使他们能够更高效地构建高质量的Android应用程序。无论是初学者还是有经验的开发者,该指南都是一个值得阅读和参考的资源。 ### 回答3: 《Jetpack架构组件从入门到精通》.pdf 是一本介绍Android Jetpack架构组件的电子书,它有助于开发者学习和理解如何使用这些组件来构建高质量、稳定的Android应用程序。 Jetpack架构组件是由谷歌开发的一组库,旨在帮助开发者简化Android应用的开发过程。它提供了一系列的工具和组件,涵盖了各个方面,包括界面设计、数据库、网络通信、数据绑定、后台处理等。 这本电子书从入门到精通地介绍了各个组件的使用方法和最佳实践。它首先详细介绍了Jetpack架构组件的核心概念和优势,让读者了解为什么要使用这些组件。 然后,电子书逐一介绍了常用的Jetpack组件,如Lifecycle、ViewModel、Room、LiveData、Navigation等。每个组件都被详细地讲解,包括其作用、使用方法和示例代码。读者可以通过跟随书中的案例来实际操作和理解这些组件的使用。 除了介绍各个组件,电子书还分享了一些进阶的使用技巧和开发经验。这些技巧包括如何优化应用性能、处理异步任务、实现数据缓存等。通过这些实用的技巧,开发者可以进一步提高应用的质量和用户体验。 总的来说,《Jetpack架构组件从入门到精通》.pdf 是一本很有价值的学习资料,对于想要深入学习和掌握Jetpack架构组件的开发者来说是必不可少的参考书籍。无论是初学者还是有经验的开发者,都可以从中获得知识和技能的提升。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值