Android Launcher抽屉类SlidingDrawer的使用

最近在研究Lanucher ,看了源码,发现了SlidingDrawer 这个类,也就是所谓的"抽屉"类。它的用法很简单,要包括handle ,和content .
handle 就是当你点击它的时候,content 要么抽抽屉要么关抽屉。别的不多说了,具体步骤如下.
1.新建Android 工程,命名为SlidingDrawer .
2.准备素材,在这里我的图标是用Launcher2 里面的图标,放在drawable-hdpi 文件夹目录结构如下:
Android Launcher抽屉类SlidingDrawer的使用
3.设置main.xml 布局:代码如下:
view plaincopy to clipboardprint?
 
 
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#808080"
  7. >
  8. <SlidingDrawer
  9. android:id="@+id/slidingdrawer"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent"
  12. android:orientation="vertical"
  13. android:handle="@+id/handle"
  14. android:content="@+id/content">
  15. <Button
  16. android:id="@+id/handle"
  17. android:layout_width="88dip"
  18. android:layout_height="44dip"
  19. android:background="@drawable/handle"
  20. />
  21. <LinearLayout
  22. android:id="@+id/content"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"
  25. android:background="#00ff00">
  26. <Button
  27. android:id="@+id/button"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="Button"
  31. />
  32. <EditText
  33. android:id="@+id/editText"
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. />
  37. </LinearLayout>
  38. </SlidingDrawer>
  39. </LinearLayout>
  40. <?xmlversion="1.0"encoding="utf-8"?>
  41. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  42. android:orientation="vertical"
  43. android:layout_width="fill_parent"
  44. android:layout_height="fill_parent"
  45. android:background="#808080"
  46. >
  47. <SlidingDrawer
  48. android:id="@+id/slidingdrawer"
  49. android:layout_width="fill_parent"
  50. android:layout_height="fill_parent"
  51. android:orientation="vertical"
  52. android:handle="@+id/handle"
  53. android:content="@+id/content">
  54. <Button
  55. android:id="@+id/handle"
  56. android:layout_width="88dip"
  57. android:layout_height="44dip"
  58. android:background="@drawable/handle"
  59. />
  60. <LinearLayout
  61. android:id="@+id/content"
  62. android:layout_width="fill_parent"
  63. android:layout_height="fill_parent"
  64. android:background="#00ff00">
  65. <Button
  66. android:id="@+id/button"
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:text="Button"
  70. />
  71. <EditText
  72. android:id="@+id/editText"
  73. android:layout_width="fill_parent"
  74. android:layout_height="wrap_content"
  75. />
  76. </LinearLayout>
  77. </SlidingDrawer>
  78. </LinearLayout>
4.设置handle 图标的样式,在drawable 里添加handle.xml ,代码如下:
 
 
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  4. <itemandroid:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  5. <itemandroid:state_pressed="true"android:drawable="@drawable/handle_pressed"/>
  6. <itemandroid:state_focused="true"android:state_enabled="true"android:drawable="@drawable/handle_focused"/>
  7. <itemandroid:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  8. <itemandroid:state_focused="true"android:drawable="@drawable/handle_focused"/>
  9. </selector>
  10. <?xmlversion="1.0"encoding="utf-8"?>
  11. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  12. <itemandroid:state_window_focused="false"android:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  13. <itemandroid:state_pressed="true"android:drawable="@drawable/handle_pressed"/>
  14. <itemandroid:state_focused="true"android:state_enabled="true"android:drawable="@drawable/handle_focused"/>
  15. <itemandroid:state_enabled="true"android:drawable="@drawable/handle_normal"/>
  16. <itemandroid:state_focused="true"android:drawable="@drawable/handle_focused"/>
  17. </selector>
5.运行之。将会得到如下效果:
Android Launcher抽屉类SlidingDrawer的使用
Android Launcher抽屉类SlidingDrawer的使用
的比较简单呵呵,如果想深入了解,大家看Launcher 源码吧!

你可能感兴趣的:(slidingdrawer)