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

Android 抽屉效果的导航菜单实现代码实例

程序员文章站 2024-02-25 22:31:09
看了很多应用,觉得这种侧滑的抽屉效果的菜单很好。 不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而且感觉能放很多东西。...

看了很多应用,觉得这种侧滑的抽屉效果的菜单很好。

Android 抽屉效果的导航菜单实现代码实例

不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而且感觉能放很多东西。

关于实现,搜索了一下,有如下两种:

1.用slidingdrawer:http://developer.android.com/reference/android/widget/slidingdrawer.html

但是不知道为什么这个类官方不建议再继续用了:deprecated since api level 17

2.用drawerlayout:http://developer.android.com/reference/android/support/v4/widget/drawerlayout.html

guide在这里:

库的引用

首先, drawerlayout这个类是在support library里的,需要加上android-support-v4.jar这个包。

然后程序中用时在前面导入import android.support.v4.widget.drawerlayout;

如果找不到这个类,首先用sdk manager更新一下android support library,然后在android sdk\extras\android\support\v4路径下找到android-support-v4.jar,复制到项目的libs路径,将其add to build path.

代码1

布局:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <android.support.v4.widget.drawerlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- the main content view -->
    <!-- main content must be the first element of drawerlayout because it will be drawn first and drawer must be on top of it -->

    <framelayout
      android:id="@+id/content_frame"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

    <!-- the navigation drawer -->

    <listview
      android:id="@+id/left_drawer"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="left"
      android:background="#111"
      android:choicemode="singlechoice"
      android:divider="@android:color/transparent"
      android:dividerheight="0dp" />
  </android.support.v4.widget.drawerlayout>

</relativelayout>

drawerlayout的第一个子元素是主要内容,即抽屉没有打开时显示的布局。这里采用了一个framelayout,里面什么也没放。

drawerlayout的第二个子元素是抽屉中的内容,即抽屉布局,这里采用了一个listview。

主要的activity(从官方实例中扒出来的):

package com.example.hellodrawer;

import android.os.bundle;
import android.app.activity;
import android.content.res.configuration;
import android.view.menuitem;
import android.view.view;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.arrayadapter;
import android.widget.listview;
import android.support.v4.app.actionbardrawertoggle;
import android.support.v4.view.gravitycompat;
import android.support.v4.widget.drawerlayout;

public class hellodraweractivity extends activity
{

  private string[] mplanettitles;
  private drawerlayout mdrawerlayout;
  private actionbardrawertoggle mdrawertoggle;
  private listview mdrawerlist;

  @override
  public void oncreate(bundle savedinstancestate)
  {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_hello_drawer);

    mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);

    // init the listview and adapter, nothing new
    initlistview();

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mdrawerlayout.setdrawershadow(r.drawable.drawer_shadow,
        gravitycompat.start);

    mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout,
        r.drawable.ic_drawer, r.string.drawer_open,
        r.string.drawer_close)
    {

      /** called when a drawer has settled in a completely closed state. */
      public void ondrawerclosed(view view)
      {

        invalidateoptionsmenu(); // creates call to
                      // onprepareoptionsmenu()
      }

      /** called when a drawer has settled in a completely open state. */
      public void ondraweropened(view drawerview)
      {

        invalidateoptionsmenu(); // creates call to
                      // onprepareoptionsmenu()
      }
    };

    // set the drawer toggle as the drawerlistener
    mdrawerlayout.setdrawerlistener(mdrawertoggle);

    // enable actionbar app icon to behave as action to toggle nav drawer
    getactionbar().setdisplayhomeasupenabled(true);
    // getactionbar().sethomebuttonenabled(true);
    // note: getactionbar() added in api level 11
  }

  private void initlistview()
  {
    mdrawerlist = (listview) findviewbyid(r.id.left_drawer);

    mplanettitles = getresources().getstringarray(r.array.planets_array);

    // set the adapter for the list view
    mdrawerlist.setadapter(new arrayadapter<string>(this,
        r.layout.list_item, mplanettitles));
    // set the list's click listener
    mdrawerlist.setonitemclicklistener(new onitemclicklistener()
    {

      @override
      public void onitemclick(adapterview<?> parent, view view,
          int position, long id)
      {
        // highlight the selected item, update the title, and close the
        // drawer
        mdrawerlist.setitemchecked(position, true);
        settitle(mplanettitles[position]);
        mdrawerlayout.closedrawer(mdrawerlist);
      }
    });
  }

  @override
  protected void onpostcreate(bundle savedinstancestate)
  {
    super.onpostcreate(savedinstancestate);
    // sync the toggle state after onrestoreinstancestate has occurred.
    mdrawertoggle.syncstate();
  }

  @override
  public void onconfigurationchanged(configuration newconfig)
  {
    super.onconfigurationchanged(newconfig);
    mdrawertoggle.onconfigurationchanged(newconfig);
  }

  @override
  public boolean onoptionsitemselected(menuitem item)
  {
    // pass the event to actionbardrawertoggle, if it returns
    // true, then it has handled the app icon touch event
    if (mdrawertoggle.onoptionsitemselected(item))
    {
      return true;
    }
    // handle your other action bar items...

    return super.onoptionsitemselected(item);
  }

}

比较纠结的是用了level 11的一个api,这样minsdkversion就有限制,不能太低。

图片资源android官网示例处提供下载了。

程序运行后效果如下:

抽屉打开前:

Android 抽屉效果的导航菜单实现代码实例

抽屉打开后:

Android 抽屉效果的导航菜单实现代码实例

代码2

今天又看了一下drawerlayout的类,发现有很多方法可以直接用的。

重新试了一下,其实不用上面那么麻烦,随便自己定义一个按钮控制抽屉的打开就行:

布局:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context=".draweractivity" >

  <android.support.v4.widget.drawerlayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- the main content view -->

    <framelayout
      android:id="@+id/content_frame"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

      <button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="open" 
        />
    </framelayout>

    <!-- the navigation drawer -->

    <listview
      android:id="@+id/left_drawer"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:background="#111"
      android:choicemode="singlechoice"
      android:divider="@android:color/transparent"
      android:dividerheight="0dp" />
  </android.support.v4.widget.drawerlayout>

</relativelayout>

主要代码:

package com.example.hellodrawer;

import android.os.bundle;
import android.app.activity;
import android.support.v4.widget.drawerlayout;
import android.view.gravity;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;

public class draweractivity extends activity
{
  private drawerlayout mdrawerlayout = null;

  @override
  protected void oncreate(bundle savedinstancestate)
  {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_drawer);

    mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);

    button button = (button) findviewbyid(r.id.btn);
    button.setonclicklistener(new onclicklistener()
    {

      @override
      public void onclick(view v)
      {
        // 按钮按下,将抽屉打开
        mdrawerlayout.opendrawer(gravity.left);

      }
    });
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。