• 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官方文档并深入钻研

  • 相关阅读:
    【史上最全】DDoS防御产品服务合集
    DDoS攻击与防御的心得体会
    防范DDoS攻击的15个方法
    什么是DDoS攻击?
    蜜罐技术详解
    全年DDoS攻击分析|知道创宇云安全2018年度网络安全态势报告
    抗D十招:十个方法完美解决DDoS攻击防御难题
    DDOS 攻击的防范教程--转载自阮一峰的博客
    手把手教你查看网站遭受到的Web应用攻击类型
    mlbox ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
  • 原文地址:https://www.cnblogs.com/cxsy/p/5635326.html
Copyright © 2020-2023  润新知