查看: 652|回复: 0

[Android教程] android.support.v4.widget.DrawerLayout 抽屉效果导航菜单

发表于 2018-1-30 08:00:01

抽屉效果导航菜单图示

如图所示,抽屉效果的导航菜单不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面左上角的一个按钮点击,菜单就滑出来,而且感觉能放很多东西

概况:实现上图所示的抽屉效果的导航菜单有以下两种方式
  方式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在这里:

  http://developer.android.com/training/implementing-navigation/nav-drawer.html

代码:android.support.v4.widget.DrawerLayout 实现抽屉效果的导航菜单

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/drawer_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <!-- 主要内容的视图-->
  7. <!-- main content must be the first element of DrawerLayout because it will be drawn first and drawer must be on top of it -->
  8. <FrameLayout android:id="@+id/content_frame"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" />
  11. <!-- 导航菜单 -->
  12. <ListView android:id="@+id/left_drawer"
  13. android:layout_width="240dp"
  14. android:layout_height="match_parent"
  15. android:layout_gravity="left"
  16. android:background="#111"
  17. android:choiceMode="singleChoice"
  18. android:divider="@android:color/transparent"
  19. android:dividerHeight="0dp" />
  20. </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;

  1. @Override
  2. public void onCreate(Bundle savedInstanceState)
  3. {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_hello_drawer);
  6. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  7. // init the ListView and Adapter, nothing new
  8. initListView();
  9. // set a custom shadow that overlays the main content when the drawer
  10. // opens
  11. mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
  12. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer,
  13. R.string.drawer_open,R.string.drawer_close){
  14. /** Called when a drawer has settled in a completely closed state. */
  15. public void onDrawerClosed(View view)
  16. {
  17. invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() }
  18. /** Called when a drawer has settled in a completely open state. */
  19. public void onDrawerOpened(View drawerView)
  20. {
  21. invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() }
  22. };
  23. // Set the drawer toggle as the DrawerListener
  24. mDrawerLayout.setDrawerListener(mDrawerToggle);
  25. // enable ActionBar app icon to behave as action to toggle nav drawer
  26. getActionBar().setDisplayHomeAsUpEnabled(true);
  27. // getActionBar().setHomeButtonEnabled(true);
  28. // Note: getActionBar() Added in API level 11
  29. }
  30. private void initListView()
  31. {
  32. mDrawerList = (ListView) findViewById(R.id.left_drawer);
  33. mPlanetTitles = getResources().getStringArray(R.array.planets_array);
  34. // Set the adapter for the list view
  35. mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, mPlanetTitles));
  36. // Set the list's click listener
  37. mDrawerList.setOnItemClickListener(new OnItemClickListener()
  38. {
  39. @Override
  40. public void onItemClick(AdapterView<?> parent, View view,int position, long id)
  41. {
  42. // Highlight the selected item, update the title, and close the
  43. // drawer
  44. mDrawerList.setItemChecked(position, true);
  45. setTitle(mPlanetTitles[position]);
  46. mDrawerLayout.closeDrawer(mDrawerList);
  47. }
  48. });
  49. }
  50. @Override
  51. protected void onPostCreate(Bundle savedInstanceState)
  52. {
  53. super.onPostCreate(savedInstanceState);
  54. // Sync the toggle state after onRestoreInstanceState has occurred.
  55. mDrawerToggle.syncState();
  56. }
  57. @Override
  58. public void onConfigurationChanged(Configuration newConfig)
  59. {
  60. super.onConfigurationChanged(newConfig);
  61. mDrawerToggle.onConfigurationChanged(newConfig);
  62. }
  63. @Override
  64. public boolean onOptionsItemSelected(MenuItem item)
  65. {
  66. // Pass the event to ActionBarDrawerToggle, if it returns
  67. // true, then it has handled the app icon touch event
  68. if (mDrawerToggle.onOptionsItemSelected(item))
  69. { return true;
  70. }
  71. // Handle your other action bar items...
  72. return super.onOptionsItemSelected(item);
  73. }
复制代码

}

  比较纠结的是用了Level 11的一个API,(Android API 11对应的是android 3.0)这样minSdkVersion就有限制,android版本不能太低。

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

  程序运行后效果如下:

  抽屉打开前:

  抽屉打开后:

库的引用
DrawerLayout这个类是在Support Library里的,需要加上android-support-v4.jar这个包。

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

  1. 如果找不到这个类,首先用SDK Manager更新一下Android Support Library(如果您采用的开发工具是android studio而非eclipse,则应该安装Android Support Repository),然后在Android SDK\extras\android\support\v4路径下找到android-support-v4.jar,复制到项目的libs路径,将其Add to Build Path.
复制代码

参考
关于Android Support Library 详情可阅读:http://blog.csdn.net/crazybigfish/article/details/18554201



回复

使用道具 举报