技术标签: android android studio kotlin
目录
导航是指支持用户导航、进入和退出应用中不同内容片断的交互。Android Jetpack的导航组件可以帮助实现导航,无论是简单的按钮点击,还是应用栏和抽屉式导航栏等更为复杂的模式,该组件均可实现。
导航组件由以下三部分组成:
导航发生在应用中的各个目的地(即您的应用中用户可以导航到的任意位置)之间。这些目的地是通过操作连接的。
导航图是一种资源文件,其中包含您的所有目的地和操作。该图表会显示应用的所有导航路径。
图 1 直观显示了一个示例应用的导航图,该应用包含 6 个目的地(通过 5 个操作连接)。每个目的地均由一个预览缩略图表示,连接操作由箭头表示,该箭头表示用户可以如何从一个目的地导航到另一个目的地。
图 1. 一个导航图,显示了由 5 个操作连接的 6 个不同目的地的预览。
如需向项目添加导航图,请执行以下操作:
res
目录,然后依次选择 New > Android Resource File。此时系统会显示 New Resource File 对话框。当您添加首个导航图时,Android Studio 会在 res
目录内创建一个 navigation
资源目录。该目录包含您的导航图资源文件(例如 nav_graph.xml
)。
注意:向您的项目添加导航图时,如果您尚未将导航依赖项添加到应用的
build.gradle
文件中,Android Studio 会显示一条提示,并为您提供添加依赖项的选项。但请注意,Android Studio 3.4 添加了非 KTX 1.0.0 版本的依赖项,因此,如果您使用的是 Kotlin 或打算使用 2.0.0 或更高版本,请务必替换这些值。
注意:Navigation 组件需要 Android Studio 3.3 或更高版本,并且依赖于 Java 8 语言功能。
def nav_version = "2.5.3"
//kotlin
implementation ("androidx.navigation:navigation-fragment-ktx:$nav_version")
implementation ("androidx.navigation:navigation-ui-ktx:$nav_version")
//Feature module Support
implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version")
//Testing Navigation
androidTestImplementation("androidx.navigation:navigation-testing:$nav_version")
(1)首先我们需要创建一个文件夹Fragment其中包含三个class,分别为LoginFragment、RegisterFragment、ForgetFragment。然后创建一个BaseFragment继承Fragment。
BaseFragment
package com.example.myapplication.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
//设置为抽象类
abstract class BaseFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//加载View
val rootView = inflater.inflate(getLayoutResId(),container,false)
//初始化
initView(rootView)
return rootView
}
open fun initView(rootView: View) {
}
//创建一个抽象方法,返回值为int类型
abstract fun getLayoutResId() : Int
}
(2)在layout中创建三个页面的布局文件fragment_login.xml、fragment_register.xml、fragment_forget.xml。最后在对应的class中返回布局文件的id。
fragment_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录界面"/>
</LinearLayout>
fragment_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册界面"/>
</LinearLayout>
fragment_forget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="找回密码界面"/>
</LinearLayout>
return R.layout.fragment_login//布局文件的id
(3)接着在主布局文件中挖坑。在res中创建一个导航文件的配置清单navigation/nav_config,xml.
请注意以下几点:(官网)
android:name
属性包含 NavHost
实现的类名称。app:navGraph
属性将 NavHostFragment
与导航图相关联。导航图会在此 NavHostFragment
中指定用户可以导航到的所有目的地。app:defaultNavHost="true"
属性确保您的 NavHostFragment
会拦截系统返回按钮。请注意,只能有一个默认 NavHost
。如果同一布局(例如,双窗格布局)中有多个宿主,请务必仅指定一个默认 NavHost
。您也可以使用布局编辑器向 activity 添加 NavHostFragment
,具体操作步骤如下:
NavHostFragment
视图拖动到您的 Activity 上。NavHostFragment
相关联的相应导航图,然后点击 OK。<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/fragment_container_view"
app:navGraph="@navigation/nav_config"
app:defaultNavHost="true"/>
</FrameLayout>
(4)接下来做配置文件nav_config.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"
app:startDestination="@id/login_fragment"
android:id="@+id/nav_config">//默认显示login_fragment
<fragment
android:id="@+id/login_fragment"
android:name="com.example.myapplication.fragment.LoginFragment">
</fragment>
<fragment
android:id="@+id/forget_fragment"
android:name="com.example.myapplication.fragment.ForgetFragment">
</fragment>
<fragment
android:id="@+id/register_fragment"
android:name="com.example.myapplication.fragment.RegisterFragment">
</fragment>
</navigation>
(5)现在来做页面之间的跳转
现在初始界面fragment_login中加入两个按钮。
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toRegisterPage"
android:text="跳转到注册界面"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toForgetPage"
android:text="跳转到找回密码界面"/>
(6)按钮有了之后我们就要通过点击按钮来实现跳转。在LoginFragment中覆写initView方法。
rootView.buttonId失败的话需要在module的build中添加依赖
apply plugin: 'kotlin-android-extensions'
override fun initView(rootView: View) {
super.initView(rootView)
rootView.toRegisterPage.setOnClickListener {
//跳转到注册界面
findNavController().navigate(R.id.to_register_fragment)
}
rootView.toForgetPage.setOnClickListener {
//跳转到找回密码界面
findNavController().navigate(R.id.to_forget_fragment)
}
}
我们要从fragment_login界面跳转到fragment_register和fragment_forget界面,需要在nav_config配置文件中login_fragmentd fragment中加入两个action。该操作有一个 ID 和一个目的地属性(其中包含下一个目的地的 ID)。
action属性:
<action android:id="@+id/to_register_fragment"
app:destination="@id/register_fragment"/>
<action android:id="@+id/to_forget_fragment"
app:destination="@id/forget_fragment"/>
这时候就可以看到login_fragment两条线分别连接到register_fragment和forget_fragment,说明了它有两个去处。
这就说明我们的页面跳转功能实现已经完成了,结果如下。
1312312
原文作者:一颗橙子
原文链接:https://blog.csdn.net/qq_52967092/article/details/128144618?spm=1001.2014.3001.5502
文章浏览阅读156次。https://link.zhihu.com/?target=https%3A//github.com/AobingJava/JavaFamily针对后端的学习,可以参照以上的网址来进行学习。后端的话就是搞一些和数据库相关的内容。其实也不是很难。然后前端+后端一起搞的人叫做全栈工程师。这里后端学习路线就分享到这里。毕竟我可能不是这方面的高手,大家想要自己加餐的,就在网上进行资料搜集和整合即可。...
文章浏览阅读400次。OpenCV学习笔记记录了本人在图像处理相关学习过程中对opencv的使用心得,主要是供自己复习,但如果碰巧为你解决了问题,那就更好了。如有错误,欢迎指正。一、OpenCV的安装安装opencv可以去官网下载对应版本的包。一些朋友可能会碰到网络问题导致下载速度特慢,此处我为大家提供opencv4.1.2资源与opencv4.1.2 Tutorial离线包。具体的安装过程建议参考:VS2017配置opencv,这个教程很详细,亲测很多次可用。..._opencv 提取地面和墙面
文章浏览阅读3.7k次,点赞9次,收藏12次。一:问题描述当输入完第一值后,就未能输入后来的字符串package com.wyj.two;import java.util.Scanner;public class text { public static void main(String[] args) { Scanner in = new Scanner(System.in); int temp = in.nextInt(); System.out.println(temp); _in.nextline();、
文章浏览阅读377次。Modem调制解调器 router 路由器192.168.1.1 192.168.1.1由于天翼宽带的光猫(modem)信号真心不行,自己加了一个TP-LINK路由器1)访问硬件设备modem地址就是192.168.1.1,无线网络ID:2)访问硬件设备Router地址也是192.168.1.1我要用笔记本连接路由器的...
文章浏览阅读1.1k次。data = json.load(jsonfile) 报错json.decoder.JSONDecodeError: Expecting value如何解决?import jsonjsonfile = open('example.json')data = json.load(jsonfile)print(data['string'])代码本身并没有问题,主要是斜体样式定义的数据exam..._data = json.load(file)
文章浏览阅读2k次。文章目录前言黑名单绕过利用 LD_PRELOAD 环境变量LD_PRELOAD 简介利用条件劫持 getuid()劫持启动进程演示过程利用ShellShock(CVE-2014-6271)使用条件:原理简述演示过程php-json-bypass使用条件:原理简述利用脚本演示过程php-GC-bypass使用条件:原理简述利用脚本演示过程利用 Backtrace使用条件:原理简述利用脚本利用方法利用 Apache Mod CGI使用条件:原理简述演示过程2020De1CTF通过攻击 PHP-FPM理论:ctf_蚁剑disable_function bypass
文章浏览阅读911次。python 计算ssim_ssim python计算
文章浏览阅读261次。1 人物分类的数据集自己制作数据集;_人物分类规则集
文章浏览阅读1.4w次。组合功能在Excel中经常使用,可以很方便地将一组数据展开或者折叠当我们需要将其所在工作表进行保护时,发现之前设定的组合无法使用了 此时,就需要我们在VBA中设定相关语句:PS:该方法转自Excelhome论坛 (http://club.excelhome.net/forum.php?mod=viewthread&tid=259467) Private Sub Workbo
文章浏览阅读4.3k次,点赞3次,收藏13次。DB2支持以下两种类型的表空间: 1、 系统管理存储器表空间(SMS-SYSTEM MANAGED STORAGE) 2、 数据库管理存储器表空间(DMS-DATABASE MANAGED STORAGE) &_db2クライアント
文章浏览阅读348次。shell脚本一键部署Apache服务1.进入/opt目录下创建apache目录,然后创建一个脚本文件和一个存放安装的目录2.下载好apache服务所需要的安装包3.编写apache脚本4.验证效果1.进入/opt目录下创建apache目录,然后创建一个脚本文件和一个存放安装的目录[root@localhost opt]# lsapache[root@localhost opt]# cd apache/[root@localhost apache]# lsinstall.sh soft[ro_编写脚本完成本机apache部署
文章浏览阅读1.2k次。这是我们学的第一个全局数据流分析,但是只能说教材封装得太好了。肯定这些分析是全局数据流分析,但是以基本块为颗粒度是怎么一回事?即使分析出了基本块的 in 或者 out,对于实际的改写程序并没有办法起到指导意义,这是因为改写的颗粒度是指令级的。显然一个粗粒度的分析没办法指导一个细粒度的改写。进行块级颗粒度的数据流分析,这个部分具有全局性质,会统筹考虑 CFG 的数据流向问题。在每个块内进行指令级颗粒度的数据流分析,这个部分发生在局部,利用的是块级颗粒度分析的结果,不需要考虑全局信息。_编译技术