美文网首页
Android 侧滑栏

Android 侧滑栏

作者: 孤独的根号十二 | 来源:发表于2019-01-14 17:58 被阅读2次

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

相关文章

  • Android 侧滑栏

    DrawerLayout 继承与ViewGroup,可以当作是FramLayout,可以指定其子控件android...

  • Android封装侧滑菜单栏

    前言 Android开发过程中,我们有时会用到侧滑菜单栏的功能。在之前我已经写过一篇关于侧滑菜单栏的介绍,大家感兴...

  • Android各种侧滑栏总结

    前言 经常我们使用Android APP时,会注意到很多app都有侧滑栏,比如网易云、B站、滴滴、QQ等。 效果展...

  • Android各种侧滑栏总结

    前言 经常我们使用Android APP时,会注意到很多app都有侧滑栏,比如网易云、B站、滴滴、QQ等。 效果展...

  • 史上最简单的侧滑栏菜单

    对于侧滑栏,相信很多人都用过,下面我将用最简单的方法实现一下,侧滑栏菜单: 已经创建好了一个侧滑栏菜单,如图: 并...

  • QQ音乐侧滑栏的实现

    对于现有的实现侧滑栏的方式,主要有两种,一种是将侧滑栏作为视图控制器,用视图控制器控制侧滑栏,这个网上有很多写好的...

  • iOS 解决自定义导航栏不能侧滑返回

    iOS中,导航栏默认是可以侧滑,但是自定义导航之后,侧滑返回失效。 解决办法:在自定义导航栏中增加侧滑手势判断,代...

  • MMDrawerController 侧滑栏与内容ScrollV

    起因:公司有个项目包含左边侧滑栏,内容是一个CollectionView嵌套多个View 侧滑栏用的是MMDraw...

  • DrawerNavigator嵌套stackNavigator嵌

    DrawerNavigator嵌套stackNavigator嵌套BottomTabNavigator,实现侧滑栏...

  • Flutter 侧滑栏及城市选择UI的实现

    Flutter 侧滑栏及城市选择UI的实现 前言   目前移动市场上很多业务都需要开发Android/IOS两个端...

网友评论

      本文标题:Android 侧滑栏

      本文链接:https://www.haomeiwen.com/subject/qlyjdqtx.html