欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android DrawerLayout实现侧拉菜单功能

程序员文章站 2023-12-06 00:01:04
效果图 activity_main.xml ...

效果图

Android DrawerLayout实现侧拉菜单功能

Android DrawerLayout实现侧拉菜单功能

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  xmlns:app="http://schemas.android.com/apk/res-auto" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  tools:context="com.example.lenovo.mydrawerlayout.mainactivity"> 
<android.support.v4.widget.drawerlayout 
  android:layout_width="match_parent" 
  android:id="@+id/drawer_layout" 
  android:layout_height="match_parent"> 
  <linearlayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 
    <android.support.v7.widget.toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionbarsize" 
      android:background="?attr/colorprimary" 
      app:popuptheme="@style/apptheme.popupoverlay" /> 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="这是主界面" 
      /> 
  </linearlayout> 
  <linearlayout 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="left" 
    android:background="#f00" 
    > 
    <textview 
      android:id="@+id/t" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="这是侧拉界面" 
      /> 
  </linearlayout> 
</android.support.v4.widget.drawerlayout> 
</linearlayout> 

注:drawerlayout下有两个布局,drawerlayout的第一个子元素是主要内容,即抽屉没有打开时显示的布局,drawerlayout的第二个子元素是抽屉中的内容,即抽屉布局,layout_gravity为侧拉方向

因为用到toolbar所以要把actionbar替换了所以设置

values下设置styles.xml    为noactionbar

<resources> 
   <!-- base application theme. --> 
  <style name="apptheme" parent="theme.appcompat.light.noactionbar"> 
    <!-- customize your theme here. --> 
    <item name="colorprimary">@color/colorprimary</item> 
    <item name="colorprimarydark">@color/colorprimarydark</item> 
    <item name="coloraccent">@color/coloraccent</item> 
  </style> 
  <style name="apptheme.noactionbar"> 
    <item name="windowactionbar">false</item> 
    <item name="windownotitle">true</item> 
  </style> 
  <style name="apptheme.appbaroverlay" parent="themeoverlay.appcompat.dark.actionbar" /> 
  <style name="apptheme.popupoverlay" parent="themeoverlay.appcompat.light" /> 
</resources> 

mainactivity

package com.example.lenovo.mydrawerlayout; 
 import android.os.bundle; 
import android.support.design.widget.snackbar; 
import android.support.v4.view.gravitycompat; 
import android.support.v4.widget.drawerlayout; 
import android.support.v7.app.actionbardrawertoggle; 
import android.support.v7.app.appcompatactivity; 
import android.support.v7.widget.toolbar; 
import android.view.view; 
import android.widget.textview; 
public class mainactivity extends appcompatactivity { 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    //toolbar代替actionbar 
    toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); 
    setsupportactionbar(toolbar); 
    // drawerlayout 
    drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); 
    //toolbar上面最左边显示三杠图标监听drawerlayout 
    actionbardrawertoggle toggle = new actionbardrawertoggle( 
        this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); 
    drawer.setdrawerlistener(toggle); 
    toggle.syncstate(); 
    //侧拉页面字体 
    textview t= (textview) findviewbyid(r.id.t); 
    t.setonclicklistener(new view.onclicklistener() { 
      @override 
      public void onclick(view v) { 
        snackbar.make(v, "侧拉页面", snackbar.length_long) 
            .setaction("action", null).show(); 
      } 
    }); 
  } 
  @override 
  public void onbackpressed() { 
    drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); 
    if (drawer.isdraweropen(gravitycompat.start)) { 
      drawer.closedrawer(gravitycompat.start); 
    } else { 
      super.onbackpressed(); 
    } 
  } 
}