效果图

activity_main.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.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"> <ImageView
android:layout_width="match_parent"
android:layout_height="355dp"
android:background="@mipmap/ic_main_background" /> <RelativeLayout
android:id="@+id/titleRelativeLayout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="18dp"> <FrameLayout
android:id="@+id/userFrameLayout"
android:layout_width="45dp"
android:layout_height="match_parent"> <ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:src="@mipmap/ic_user" />
</FrameLayout> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题"
android:textColor="#fff0f0f0"
android:textSize="18sp" />
</RelativeLayout> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:text="DrawerLayout演示"
android:textColor="#fff0f0f0"
android:textSize="25sp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="500dp"
android:text="This is a stub."
android:textAllCaps="false" />
</RelativeLayout> <LinearLayout
android:id="@+id/leftLinearLayout"
android:layout_width="233dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffffff"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="39dp"
android:layout_marginBottom="23dp"> <ImageView
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="16dp"
android:src="@mipmap/ic_portrait" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="布同林"
android:textColor="#ff1b1b1b"
android:textSize="18sp" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffdfdfdf" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:drawableLeft="@mipmap/ic_profile"
android:drawablePadding="17dp"
android:paddingTop="30dp"
android:text="我的资料"
android:textColor="#ff1b1b1b"
android:textSize="14sp" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:drawableLeft="@mipmap/ic_message"
android:drawablePadding="17dp"
android:paddingTop="27dp"
android:text="我的消息"
android:textColor="#ff1b1b1b"
android:textSize="14sp" /> <TextView
android:id="@+id/aboutUsTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:drawableLeft="@mipmap/ic_information"
android:drawablePadding="17dp"
android:paddingTop="27dp"
android:text="关于我们"
android:textColor="#ff1b1b1b"
android:textSize="14sp" /> <Button
android:id="@+id/closeDrawerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:text="关闭" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

MainActivity.java

package com.bu_ish.empty;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window; public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.TRANSPARENT);
}
drawerLayout = findViewById(R.id.drawerLayout);
findViewById(R.id.userFrameLayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
findViewById(R.id.aboutUsTextView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setMessage("布同林").show();
}
});
findViewById(R.id.closeDrawerButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawer(Gravity.LEFT);
}
});
}
}

P.S.

左侧划出菜单,则android:layout_gravity="left"

DrawerLayout.openDrawer(int gravity):打开菜单

DrawerLayout.closeDrawer(int gravity):关闭菜单

完整Demo

链接:https://pan.baidu.com/s/16tsE4jiQWbTdGSNKBHK-4g
提取码:cd25

Android笔记之DrawerLayout的基本使用的更多相关文章

  1. Android笔记:DrawerLayout抽屉布局的使用

    DrawerLayout(抽屉布局),在各种app中经常出现,比如csdn.. 如下示,只要从屏幕侧边滑一下,或者点击左上角的图标,抽屉就会出来. DrawerLayout要点: 1.使用Drawer ...

  2. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  3. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  4. Android抽屉效果 DrawerLayout 入门经验总结

    今天试了试这个抽屉布局的效果,结果很崩溃无语 网上很多资料都千篇一律,感觉都有问题,下面总结下几点经验: 先上个效果图: 1.  layout 布局文件中怎么写: <android.suppor ...

  5. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  6. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  7. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  8. Android笔记--View绘制流程源码分析(二)

    Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...

  9. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

随机推荐

  1. Echarts-之显示百分比

    对于使用echarts要显示百分比,要改两个地方,第一个地方时坐标轴显示为百分比的格式,第二个是让值以百分比的形式显示,如50,在图上面显示为50%. yAxis: [ { type: 'value' ...

  2. Codefroces Gym101572 I.Import Spaghetti-有向图跑最小环输出路径(Floyd)

    暑假学的很多东西,现在都忘了,补这道题还要重新学一下floyd,有点难过,我暑假学的东西呢??? 好了,淡定,开始写题解. 这个题我是真的很难过啊,输入简直是有毒啊(内心已经画圈诅咒出题人无数次了.. ...

  3. linux下kill某个应用

    linux命令行与桌面切换快捷键Ctr+Alt+F1,Ctr+Alt+F7 ps -e | grep abc sudo kill xyz

  4. 同源策略Same-origin policy

     同源策略Same-origin policy 同源策略Same-origin policy是Web应用的一种安全基础策略.它规定同一源中,页面包含的脚本可以访问该源下的其他页面的数据.只有当网址中的 ...

  5. eclipse默认配色(内含恢复文件和恢复方法)

    转载:http://blog.csdn.net/w174504744/article/details/8672679 很多搞开发的同学一开始不喜欢默认的eclipse白底配色,去网上千辛万苦搜到了很多 ...

  6. Android 沉浸式全屏

    Android 4.4 带来了沉浸式全屏体验, 在沉浸式全屏模式下, 状态栏. 虚拟按键动态隐藏, 应用可 以使用完整的屏幕空间, 按照 Google 的说法, 给用户一种 “身临其境” 的体验. A ...

  7. 【hibernate postgresql】注解@TypeDef/@Enumerated/数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" is of type gender but expression is of type character varying

    数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" ...

  8. mac下报错 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    如题mac下遇到错误: 解决办法:安装mac的命令行工具CommandLineTools xcode-select --install

  9. 纯CSS3美化radio和checkbox

    如题,主要通过CSS3来实现将radio和checkbox美化的效果.可是兼容性并非非常好,PC端仅仅支持chrome浏览器(IE和Firefox測试不行,其它没有很多其它測试).然后微信端和QQ端訪 ...

  10. nexus启动报错-----&gt;错误 1067: 进程意外终止。

    1.今天启动nexus报错: 2.错误信息 错误 1067: 进程意外终止. 3.检查发现我之前把jdk升级了. 然而nexus之前指定的jdk将不再生效. 4.解决的方法 找到nexus安装文件夹 ...