使用DrawerLayout和SlidingMenu分别实现侧滑菜单、以及AS使用SlidingMenu第三方库的步骤
最近有需求要实现一下侧滑菜单的功能,网上有很多类似的案例,但不是不美观就是失败率极高。所以自己花了点时间实现了一下,然后展示出来,算做一下笔记。
AS导入SlidingMenu第三方库的步骤
SlidingMenu的Github地址:https://github.com/jfeinstein10/SlidingMenu.
1、下载下来之后,首先拷贝SlidingMenu-master的library文件夹到你项目的根目录下![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ea1afc5e04fac3bf03714379765d07ad.png)
2、修改setting.gradle
在项目的setting.gradle里面添加module名字
include ':app',':SlidingMenuLibrary'
3、修改app目录下的build.gradle
添加引用momodule
implementation project(':SlidingMenuLibrary')
完了点击右上角的Sync Now,让AS自动为SlidingMenu自动生成gradle,会报错误,没有关系,继续修改错误
4、修改SlidingMenuLibrary的build.gradle
修改的参数跟APP下的build.gradle一致,我的项目是修改成这样:
然后点击右上角的Try again,重新刷新项目,还会报错误,继续修改
5.继续修改错误
以上的步骤就是AS正确导入第三方库SlidingMenu的操作,接下来就操作一个新项目
用SlidingMenu第三方库实现侧滑菜单
很简单,先来个左侧滑菜单栏布局left_menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Layne"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="了解会员特权"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="个人装扮"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="我的收藏"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="我的相册"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="我的文件"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="免流量特权"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
差不多的右布局right_menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="设置"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="个人信息"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="二维码名片"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="我的帐号"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="聊天背景"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="通用"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="黑名单"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="隐私"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="系统通知"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_item_style"
android:clickable="true"
android:drawableRight="@drawable/mm_submenu_normal"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="帮助与反馈"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:background="@drawable/list_item_style"
android:gravity="center"
android:text="退出登录"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
主页面MainActivity的代码:
public class MainActivity extends SlidingFragmentActivity {
private SlidingMenu slidingMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置左侧侧拉条目的布局
setBehindContentView(R.layout.left_menu_main);
// 获取侧拉条目对象
slidingMenu = getSlidingMenu();
// 1、设置侧拉条目的方向;
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
// 2、设置内容页的宽度;
slidingMenu.setBehindOffsetRes(R.dimen.main_menu_behind_offset);
// 设置侧拉条目的宽度
// slidingMenu.setBehindWidthRes(res)
// 3、设置侧拉条目和内容页分隔线,颜色渐变
slidingMenu.setShadowDrawable(R.drawable.shape_shadow);
// 指明分隔线宽度
slidingMenu.setShadowWidthRes(R.dimen.main_menu_shadow_width);
// 4、指明可拖拽侧拉条目的范围
// 全屏可拖拽
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// 边缘可拖拽
// slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
// 全屏不可拖拽
// slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
// 设置右边侧拉
slidingMenu.setSecondaryMenu(R.layout.right_menu_main);
// 设置右边侧拉分隔线
slidingMenu.setSecondaryShadowDrawable(R.drawable.shape_shadow);
}
}
运行结果: