安卓NavigationView使用方法

2023-12-20 06:10

本文主要是介绍安卓NavigationView使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

导入依赖

介绍NavigationView

布局文件、标签属性


导入依赖

使用Design下的NavigationView前导入依赖步骤

在这个页面中直接找到design这个依赖,点击后再点击这两个窗口上的OK按钮,等待编译完成就完成导入了!

(可能版本不一样,导入即可)

 

13.1.5 介绍NavigationView

Google在5.0之后推出的NavitationView,所有的布局控件放在DrawerLayout中来使用,NavigationView的作用就像这样,实现侧拉效果。和DrawerLayout效果几分差异,QQ从左至右从边滑动的侧拉效果。上半部分图片背景,圆形头像,下面每一个都是导航菜单现在就来实现一下它(虽然是Iphone图,但是没关系了,就是这个道理)。


布局文件、标签属性

敲出最简单的布局,实现最简单的效果

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"><LinearLayoutandroid:background="#4d7aed"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:gravity="center"android:textSize="18sp"android:text="这里是一个主页面"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout><android.support.design.widget.NavigationViewandroid:layout_gravity="left"android:background="#ffffff"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

将所有布局控件放下DrawerLayout 中,NavigationView加上属性android:layout_gravity="left"实现左侧滑!


如果加一个头布局文件,姐可以侧拉出这样的效果布局代码如下

主布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#4d7aed"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="这里是一个主页面"android:textSize="18sp" /></LinearLayout><android.support.design.widget.NavigationViewapp:headerLayout="@layout/layout"android:layout_width="450dp"android:layout_height="match_parent"android:layout_gravity="start"android:background="#ffffff"></android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

 

头布局文件layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="260dp"android:orientation="vertical"><ImageViewandroid:layout_width="match_parent"android:layout_height="230dp"android:background="@mipmap/kb" /><TextViewandroid:layout_width="match_parent"android:layout_height="30dp"android:background="#63a6e9"android:gravity="center"android:text="《魁拔》"android:textColor="#fff"android:textSize="18sp" /></LinearLayout>

之后加上menu就可以实现这样的效果了(虽然丑了,但是还是可以实现,要求美观就细扣)

创建的menu应该在这里

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"><group android:checkableBehavior="single"><itemandroid:id="@+id/item1"android:title="功能列表1"></item><itemandroid:id="@+id/item2"android:title="功能列表2"></item><itemandroid:id="@+id/item3"android:title="功能列表3"></item><itemandroid:id="@+id/item4"android:title="功能列表4"></item><itemandroid:id="@+id/item5"android:title="功能列表5"></item></group>
</menu>

还可以在每个item加上  android:icon=""  ,为这个item设置上图标

 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#4d7aed"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="这里是一个主页面"android:textSize="18sp" /></LinearLayout><android.support.design.widget.NavigationViewapp:menu="@menu/menu"app:headerLayout="@layout/layout"android:layout_width="450dp"android:layout_height="match_parent"android:layout_gravity="start"android:background="#ffffff"></android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

头布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="260dp"android:orientation="vertical"><ImageViewandroid:layout_width="match_parent"android:layout_height="230dp"android:background="@mipmap/kb" /><TextViewandroid:layout_width="match_parent"android:layout_height="30dp"android:background="#63a6e9"android:gravity="center"android:text="《魁拔》"android:textColor="#fff"android:textSize="18sp" /></LinearLayout>

 


NavitationView常用属性

item加入点击事件

当布局文件中app:menu="@menu/item_menu"加入后,要为对应的item加上点击事件,这时候用到的是setNavigationItemSelectedListener

使用方法:

实例化NavigationView后,之后直接调用这个方法,就像如下代码

实例化的NavigationView控件.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {switch (menuItem.getItemId()) {case R.id.title1:Toast.makeText(Main2Activity.this, "第一个", Toast.LENGTH_SHORT).show();break;}return true;}});

这样就加入点击事件了,switch中对应着加入点击事件

》》》》》》快速实例化控件的插件的安装与使用《《《《《《

这篇关于安卓NavigationView使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/514999

相关文章

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

关于Mybatis和JDBC的使用及区别

《关于Mybatis和JDBC的使用及区别》:本文主要介绍关于Mybatis和JDBC的使用及区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、JDBC1.1、流程1.2、优缺点2、MyBATis2.1、执行流程2.2、使用2.3、实现方式1、XML配置文件

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增