zoukankan      html  css  js  c++  java
  • Android开发之NavigationView的使用

    NavigationView主要是和DrawerLayout框架结合使用,来完成抽屉导航实现侧边栏

    引用一段官方文档的示例代码

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true">
         <!-- Your contents -->
         <android.support.design.widget.NavigationView
             android:id="@+id/navigation"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:layout_gravity="start"
             app:menu="@menu/my_navigation_items" />
     </android.support.v4.widget.DrawerLayout>

    在使用NavigationView之前需要将相应的design库添加到项目的依赖中,

    然后在xml中外层用DrawerLayout包裹,内层一部分是正文内容区(content),另一部分则是侧边栏NavigationView,这里面包含两个布局,一个是headerLayout,一个是menu,

    头布局没什么好说的,跟普通的布局定义方式类似,menu布局的示例代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <group android:checkableBehavior="single">
    <item android:id="@+id/navigation_item_first" android:icon="@drawable/first" android:title="第一项" />
    <item android:id="@+id/navigation_item_second" android:icon="@drawable/second" android:title="第二项" />
    <item android:id="@+id/navigation_item_third" android:icon="@drawable/third" android:title="第三项" />
    </group>
    </menu>

    其中各个条目还有checked属性,设置为true的条目将会高亮显示

    group中的checkableBehavior属性表示这组这组菜单是否checkable,有三个值可选,分别为none,all(单选/单选按钮radio button),single(非单选/复选类型checkboxes)

    最后还可以在代码中通过setNavigationItemSelectedListener方法来设置菜单项被点击的回调,

    里面重写的onNavigationItemSelectedListener方法提供了被选中的MenuItem,用法和Activity的onOptionsItemSelected类似

    这样就可以实现一个简单的抽屉导航侧滑菜单栏,需要更多的属性与用法可以自行查看Android官方文档并深入钻研

  • 相关阅读:
    rpcbind禁用不了,111端口无法释放?
    python3 pexpect 自动交互修改linux系统密码
    pip将包安装到指定位置
    使用openssl对文件进行加密解密
    james-2.3.2.1发邮件慢,不能多线程同时发
    java指令执行一个class文件并指定依赖包的路径
    ltib for imx280 tips
    使用net-snmp的snmptranslate命令验证MIB文件
    嵌入式linux去掉开机进度条,更换背景,换企鹅logo
    使用cppcheck对C代码进行静态检查
  • 原文地址:https://www.cnblogs.com/cxsy/p/5635326.html
Copyright © 2011-2022 走看看