Android5.0材料设计(三)

()实现侧滑(Navigation Drawer)

之前左侧拉出 Drawer Menu 是一个空白的面板。以前需要用LinearLayout 或者 ListView 去实现,这种方式是非常麻烦的。现在可以采用NavigationView来实现,NavigationView 就是为了 Drawer Menu 而特别设计的。

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        .......
</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemBackground="@android:color/holo_red_dark"
    app:itemTextColor="#FFFF00"
    app:menu="@menu/navigation_drawer_items" />

</android.support.v4.widget.DrawerLayout>

itemBackground — 设置菜单项的背景
itemTextColor — 设置菜单项的文本颜色

其中headerLayout是标题布局文件,menu是菜单资源文件。NavigationView 将这两个资源文件绑定起来,作为 Drawer Menu 的菜单区域。

通过声明 setNavigationItemSelectedListener 来监听NavigationView菜单项的点击事件

NavigationView控件.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        int id = menuItem.getItemId();
        switch (id) {
            case R.id.XX:
                ........
                break;

            case R.id.XX:
                ........
                break;

            case R.id.XX:
                ........
                break;
        }
        return false;
    }
});

参考资料:https://github.com/MrFuFuFu/Codelab

https://www.aswifter.com/android/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值