当前位置:   article > 正文

安卓NavigationView使用方法_android navigationview

android navigationview

目录

导入依赖

介绍NavigationView

布局文件、标签属性


导入依赖

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

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

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

 

13.1.5 介绍NavigationView

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


布局文件、标签属性

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <LinearLayout
  9. android:background="#4d7aed"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12. <TextView
  13. android:gravity="center"
  14. android:textSize="18sp"
  15. android:text="这里是一个主页面"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent" />
  18. </LinearLayout>
  19. <android.support.design.widget.NavigationView
  20. android:layout_gravity="left"
  21. android:background="#ffffff"
  22. android:layout_width="match_parent"
  23. android:layout_height="match_parent">
  24. </android.support.design.widget.NavigationView>
  25. </android.support.v4.widget.DrawerLayout>

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


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

主布局文件activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#4d7aed">
  12. <TextView
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:gravity="center"
  16. android:text="这里是一个主页面"
  17. android:textSize="18sp" />
  18. </LinearLayout>
  19. <android.support.design.widget.NavigationView
  20. app:headerLayout="@layout/layout"
  21. android:layout_width="450dp"
  22. android:layout_height="match_parent"
  23. android:layout_gravity="start"
  24. android:background="#ffffff">
  25. </android.support.design.widget.NavigationView>
  26. </android.support.v4.widget.DrawerLayout>

 

头布局文件layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="260dp"
  5. android:orientation="vertical">
  6. <ImageView
  7. android:layout_width="match_parent"
  8. android:layout_height="230dp"
  9. android:background="@mipmap/kb" />
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="30dp"
  13. android:background="#63a6e9"
  14. android:gravity="center"
  15. android:text="《魁拔》"
  16. android:textColor="#fff"
  17. android:textSize="18sp" />
  18. </LinearLayout>

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

创建的menu应该在这里

menu.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <group android:checkableBehavior="single">
  4. <item
  5. android:id="@+id/item1"
  6. android:title="功能列表1"></item>
  7. <item
  8. android:id="@+id/item2"
  9. android:title="功能列表2"></item>
  10. <item
  11. android:id="@+id/item3"
  12. android:title="功能列表3"></item>
  13. <item
  14. android:id="@+id/item4"
  15. android:title="功能列表4"></item>
  16. <item
  17. android:id="@+id/item5"
  18. android:title="功能列表5"></item>
  19. </group>
  20. </menu>

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

 

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#4d7aed">
  12. <TextView
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:gravity="center"
  16. android:text="这里是一个主页面"
  17. android:textSize="18sp" />
  18. </LinearLayout>
  19. <android.support.design.widget.NavigationView
  20. app:menu="@menu/menu"
  21. app:headerLayout="@layout/layout"
  22. android:layout_width="450dp"
  23. android:layout_height="match_parent"
  24. android:layout_gravity="start"
  25. android:background="#ffffff">
  26. </android.support.design.widget.NavigationView>
  27. </android.support.v4.widget.DrawerLayout>

头布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="260dp"
  5. android:orientation="vertical">
  6. <ImageView
  7. android:layout_width="match_parent"
  8. android:layout_height="230dp"
  9. android:background="@mipmap/kb" />
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="30dp"
  13. android:background="#63a6e9"
  14. android:gravity="center"
  15. android:text="《魁拔》"
  16. android:textColor="#fff"
  17. android:textSize="18sp" />
  18. </LinearLayout>

 


NavitationView常用属性

item加入点击事件

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

使用方法:

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

  1. 实例化的NavigationView控件.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  2. @Override
  3. public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  4. switch (menuItem.getItemId()) {
  5. case R.id.title1:
  6. Toast.makeText(Main2Activity.this, "第一个", Toast.LENGTH_SHORT).show();
  7. break;
  8. }
  9. return true;
  10. }
  11. });

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

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

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/219243
推荐阅读
相关标签
  

闽ICP备14008679号