Android 侧滑栏

DrawerLayout

继承与ViewGroup,可以当作是FramLayout,可以指定其子控件android:layout_gravity属性设置左边侧滑或者右边侧滑,start是左边

addDrawerListener

可以通过设置DrawerListener来监听滑动过程中的变化

drawerlayout.addDrawerListener(new DrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {

            }
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                View content = drawerlayout.getChildAt(0);
                View menu = drawerView;
                float scale = 1-slideOffset;//1~0
                float leftScale = (float) (1-0.3*scale);
                float rightScale = (float) (0.7f+0.3*scale);//0.7~1
                menu.setScaleX(leftScale);//1~0.7
                menu.setScaleY(leftScale);//1~0.7

                content.setScaleX(rightScale);
                content.setScaleY(rightScale);
                content.setTranslationX(menu.getMeasuredWidth()*(1-scale));//0~width

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }
        });

与Toolbar组合使用

toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        
        drawerlayout = (DrawerLayout)findViewById(R.id.drawerlayout);
        
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar, 
        R.string.drawer_open, R.string.drawer_close);
        drawerToggle.syncState();

        drawerlayout.addDrawerListener(drawerToggle);//不设置这里,左边的图标不会产生动画

与NavigationView组合使用

可用于快速的制定左面的菜单栏

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lsn8_materialdesign_slidingmenu_navigationview.MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.example.lsn8_materialdesign_slidingmenu_navigationview">
    <!-- 内容部分 -->
    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />
    <!-- 菜单部分 -->
    <android.support.design.widget.NavigationView
        android:layout_gravity="start"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        app:menu="@menu/navigation_menu"
        app:headerLayout="@layout/navigation_headerlayout"
        android:background="@android:color/darker_gray"
        />
</android.support.v4.widget.DrawerLayout>

对应menu文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/action_gallery"
        android:title="相册"
        android:orderInCategory="100"
        android:icon="@android:drawable/ic_menu_gallery"
        />
    <item 
        android:id="@+id/action_details"
        android:title="详情"
        android:orderInCategory="100"
        android:icon="@android:drawable/ic_menu_info_details"
        />
    <item 
        android:id="@+id/action_about"
        android:title="关于"
        android:orderInCategory="100"
        android:icon="@android:drawable/ic_menu_help"
        />
    <item 
        android:id="@+id/action_music"
        android:title="音樂"
        android:orderInCategory="100"
        android:icon="@android:drawable/ic_menu_more"
        >
        <menu >
            <item 
                android:id="@+id/action_play"
                android:title="播放"
                android:icon="@android:drawable/ic_media_play"/>
            <item 
                android:id="@+id/action_pause"
                android:title="暫停"
                android:icon="@android:drawable/ic_media_pause"/>
        </menu>
      </item>

</menu>

github:
https://github.com/CodeHurricane/SlideDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容