一、概述

DrawerLayout是一个能够方便的实现Android側滑菜单的组件,我近期开发的项目中也有一个側滑菜单的功能。于是DrawerLayout就派上用场了。假设你从未使用过DrawerLayout。那么本篇博客将使用一个简单的案例带你迅速掌握DrawerLayout的使用方法。

二、效果图

三、代码实现

主布局activity_main.xml

<?

xml version="1.0" encoding="utf-8"?

>
<LinearLayout 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:background="@android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blueStatus"
android:minHeight="? attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator"
app:theme="@style/Theme.AppCompat.NoActionBar"> </android.support.v7.widget.Toolbar> <include layout="@layout/title_layout" /> <android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"> <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <LinearLayout
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="right"> <include layout="@layout/drawer_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

当你使用DrawerLayout的时候。DrawerLayout的第一个元素就是主要内容区域(在本案例中是ListView),它的宽高必须是match_parent。

在主要内容区域的后面加入側滑视图(在本案例中是drawer_content.xml)。而且通过设置layout_gravity来决定它是左滑还是右滑,通常这个側滑视图的高度设为match_parent。

drawer_content.xml

<?

xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"> <TextView
style="@style/NormalTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="船中文名" /> <EditText
style="@style/SmallGreyTextView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btn_round_white"
android:padding="@dimen/margin_8" /> <TextView
style="@style/NormalTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="船英文名" /> <EditText
style="@style/SmallGreyTextView" android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btn_round_white"
android:padding="@dimen/margin_8" /> <TextView
style="@style/NormalTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="呼号" /> <EditText
style="@style/SmallGreyTextView" android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btn_round_white"
android:padding="@dimen/margin_8" /> <TextView
style="@style/NormalTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="IMO" /> <EditText
style="@style/SmallGreyTextView" android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btn_round_white"
android:padding="@dimen/margin_8" /> <TextView
style="@style/NormalTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="MMSI" /> <EditText
style="@style/SmallGreyTextView" android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/btn_round_white"
android:padding="@dimen/margin_8" /> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"> <Button
android:id="@+id/btn_confirm"
style="@style/NormalGreyTextView"
android:layout_width="80dp"
android:layout_height="36dp"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:background="@drawable/btn_round_red"
android:gravity="center"
android:text="查询"
android:textColor="@color/white" />
</RelativeLayout>
</LinearLayout>

这个布局文件就是側滑视图,如图:

MainActivity.java
package com.leohan.drawerlayoutdemo;

import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @InjectView(R.id.toolbar)
Toolbar toolbar;
@InjectView(R.id.tv_search)
TextView tvSearch;
@InjectView(R.id.listView)
ListView listView;
@InjectView(R.id.drawer_layout)
DrawerLayout drawerLayout; private List data = new ArrayList();
private ShipRecordAdapter adapter = new ShipRecordAdapter(this, data); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this); setSupportActionBar(toolbar);
listView.setAdapter(adapter);
getData();
} @Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
} /**
* 获取类别数据
*/
private void getData() {
for (int i = 0; i < 6; i++) {
Map<String, Object> map = new HashMap<>();
data.add(map);
}
adapter.notifyDataSetChanged();
} @OnClick(R.id.tv_search)
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_search:
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
break;
}
}
}

因为这里的側滑视图是从右側滑动出现的,因此側滑视图的layout_gravity设置为right或者end,假设是左滑就设置为left或者start。

当我们手动控制側滑视图的打开或者关闭的时候,须要运行下面代码:

//close
drawerLayout.closeDrawer(Gravity.RIGHT);
//open
drawerLayout.openDrawer(Gravity.RIGHT);

至此DrawerLayout的基本使用就已经掌握了,更深入的了解DrawerLayout,请參考Google官方文档Creating a Navigation Drawer

项目源代码:https://github.com/leoleohan/DrawerLayoutDemo

Android 使用DrawerLayout高速实现側滑菜单的更多相关文章

  1. Android 实现形态各异的双向側滑菜单 自己定义控件来袭

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935.本文出自:[张鸿洋的博客] 1.概述 关于自己定义控件側滑已经写了 ...

  2. Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码

    Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码 左右側滑效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a ...

  3. android側滑菜单-DrawerLayout的基本使用

    眼下主流App开发中,部分是以側滑菜单为主布局架构,曾经做android側滑菜单时.大多选择使用github上的第三方开源框架SildingMenu,可是这个框架还是稍显笨重.好消息是google已经 ...

  4. 自己实现android側滑菜单

    当今的android应用设计中.一种主流的设计方式就是会拥有一个側滑菜单,以图为证:     实现这种側滑效果,在5.0曾经我们用的最多的就是SlidingMenu这个开源框架,而5.0之后.goog ...

  5. android:QQ多种側滑菜单的实现

    在这篇文章中写了 自己定义HorizontalScrollView实现qq側滑菜单 然而这个菜单效果仅仅是普通的側拉效果 我们还能够实现抽屉式側滑菜单 就像这样 第一种效果 另外一种效果 第三种效果 ...

  6. iOS分组通讯录效果+側滑菜单(MMDrawerController)

    前言的废话-能够忽略 自从学会了使用Cocoapod,就欲罢不能了!由于太简单太赞了,不用再把源代码粘到project里了! 參见戴维营博客中的解说:Cocoapod 安装以及使用 先上一下效果图,请 ...

  7. 高仿QQ6.0側滑菜单之滑动优化(二)

    好了,昨天已经实现了高仿QQ6.0的側滑大致框架.如有兴趣.能够去看下仿QQ6.0側滑之ViewDragHelper的使用(一) 可是之前的实现.仅仅是简单的能够显示和隐藏左側的菜单,可是特别生硬,并 ...

  8. Android学习之仿QQ側滑功能的实现

    如今项目越来越多的应用了滑动删除的功能,Android本来遵循的是长按删除,IOS定制的是滑动删除,不可否认滑动删除确实在客户体验上要好一点,所以看了非常多关于仿QQ滑动删除的样例,还是感觉代码家的A ...

  9. 【Android 界面效果31】Android--侧滑菜单应用的实现

    侧滑菜单应用现在非常多,而且实现方式也多种多样.通过在网上的多方查找,我找到郭霖少侠的这篇文章:http://blog.csdn.net/guolin_blog/article/details/874 ...

随机推荐

  1. HDU 3584 Cube 【 三维树状数组 】

    题意:还是那篇论文里面讲到的,三维树状数组http://wenku.baidu.com/view/1e51750abb68a98271fefaa8画个立方体出来对照一下好想一点 #include< ...

  2. POJ 3278 Catch That Cow【BFS】

    题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...

  3. 安卓、safari和微信各个浏览器的设计标准

  4. H5页面在线制作工具搜集

    1.初页 http://chuye.cloud7.com.cn/ 2.易企秀 http://eqxiu.com/#/home 3.最酷 http://www.zuiku.com/ 4.易传单 http ...

  5. 服务器搭建域控与SQL Server的AlwaysOn环境过程(三)配置故障转移

    0 引言 主要讲述如何搭建故障转移集群,因为AlwaysOn是基于Windows的故障转移集群的. 在讲解步骤之前需要了解一下故障转移集群仲裁配置 下面图片来自<Windows Server20 ...

  6. luogu P1365 WJMZBMR打osu! / Easy(期望DP)

    题目背景 原 维护队列 参见P1903 题目描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有nnn次点击要做,成功了就是o,失败了就是 ...

  7. luogu P4018 Roy&October之取石子(博弈论)

    题意 题解 如果n是6的倍数,先手必败,否则先手必胜. 因为6*x一定不是pk 所以取得话会变成6*y+a的形式a=1,2,3,4,5: 然后a一定为质数.我们把a取完就又成为了6*x的形式. 又因为 ...

  8. Ibatis使用技巧

    一.在ibatis中以Map形式返回查询结果 1.在ibatis的配置文件中配置以HashMap返回的resultMap <resultMap id="MAX_MIN_ID_RESUL ...

  9. DBCA创建数据库ORA-01034 ORACLE not available

    SYMPTOMS 在利用dbca创建数据库时,当设置完毕全部參数.開始装时 跑到2% 就报错 ORA-01034 ORACLE not available, 例如以下图 watermark/2/tex ...

  10. linux清除邮件队列

    [root@localhost mail]#tmp=`mailq | grep -E "root" | awk '{print $1}'` [root@localhost mail ...