android滑动菜单-drawerLayout

简介

DrawerLayout充当窗口内容的顶级容器,允许从窗口的一个或两个垂直边缘拉出交互式“抽屉”视图
DrawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏,主内容区的部分可以随着菜单的点击而变化。

声明

  • .xml文件中直接使用drawerLayout布局,布局中允许放两个直接子控件,第一个子控件是主屏幕中显示的内容,第二个子控件是滑动菜单显示的内容,代码如下:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:id="@+id/draw">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff0">
        </FrameLayout>
        <LinearLayout
            android:background="#ffff"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:orientation="vertical">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
    
    layout_gravity属性,用于设置滑动菜单出现在哪一侧
    注:主屏幕推荐使用碎片布局,方便页面切换管理

切换视图

  • 滑动菜单内的组件跟正常组件声明过程一样,在点击事件的回调函数中切换碎片,并关闭滑动菜单视图,代码如下
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /**
             * 碎片切换等操作
             */
            draw.closeDrawers();//关闭视图
        }
    });
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容