Android中使用DrawerLayout实现抽屉式菜单

前言:之前一直都在学习别人的博客,而很少自己写,事实上最近做到一个项目,由于学习安卓时间很短,对于安卓中一些经典的组件以及布局方式不是很熟习,因此借着此篇博客来和大家一起学习和巩固一下Android中的DrawerLayout+Naviagation+Toolbar组件的以及沉浸式状态栏的应用。


首先各组件整体应用后的效果如下图:


第一步在新建项目,将MainActivity的布局文件的根元素变为DrawerLayout,主布局文件整体代码如下:

注意:对于Navigation组件的应用,须在gradle文件

dependencies 

中添加依赖:

compile 'com.android.support:design:26.+'


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.drawerlayoutexample.MainActivity">

   <LinearLayout
       android:fitsSystemWindows="true"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
      <android.support.v7.widget.Toolbar
          app:contentInsetStart="0dp"
          android:layout_width="match_parent"
          android:background="@color/colorPrimary"
          android:layout_height="?attr/actionBarSize"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
          app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent">
         <RelativeLayout
             android:layout_weight="1"
             android:layout_width="0dp"
             android:layout_height="match_parent">
            <ImageButton
                android:layout_marginLeft="20dp"
                android:id="@+id/menu"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:background="@drawable/main_menu" />
         </RelativeLayout>
         <RelativeLayout
             android:layout_weight="1"
             android:layout_width="0dp"
             android:layout_height="match_parent"
  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值