Adroid之NavigationView

本文介绍了Android中用于应用导航的Navigation Drawer,特别是重点讨论了NavigationView的使用和设计。内容包括NavigationView的基本概念,以及如何自定义一个符合设计需求的NavigationView,通过实例展示了其在QQ主界面侧滑菜单中的应用。

Navigation drawer导航抽屉,被设计用于应用导航,提供了一种通用的导航方式。他是android5.0后引入的,其中qq的主界面的侧滑菜单也是由它衍生。以下使我我自己设计的一个NavigationView


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <!-- Content -->
    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- Drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        app:headerLayout="@layout/main"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>
其中 FrameLayout是主界面content,第二个chird是侧滑界面注意其中NavigationView的两个自定义属性
app:headerLayout接收一个layout,可选项。app:menu接收一个menu,这个是必须选择的一项。

NavigationView提供的OnNavigationItemSelectedListener的方法为我们选中的item添加点击事件。通过这个方法我们可以为我们的item提供我们需要执行的操作

 public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_scanner) {
            Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
        } else if (id == R.id.nav_add) {
            Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
        } else if (id == R.id.nav_about) {
            Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
        } else if (id == R.id.nav_search) {
            Toast.makeText(this,"点击了我",Toast.LENGTH_SHORT).toString();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlayout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值