安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)
记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下:
有兴趣的朋友可以看看这篇文件《安卓开发笔记——自定义HorizontalScrollView控件(实现QQ5.0侧滑效果)》
今天换一种实现方式,来说下GitHub上非常优秀的开源项目SlidingMenu的使用,它是一种比较新的界面效果,在主界面左滑或右滑出现设置界面效果,能方便的进行各种操作。市面上很多优秀的APP都采用了这种界面方案,像facebook、人人网、everynote、Google+等。
再来看看今天要实现的效果图:
直接进入主题吧,先来说下准备工作:
1、既然是要使用这个开源项目,那首先当然是要下载它了,这是SlidingMenu的下载地址:https://github.com/jfeinstein10/SlidingMenu
在这个项目介绍中,我们可以发现这样的两句话,首先SlidingMenu它只是作为一个库文件引入,再来它需要依赖ActionBarSherlock,所以这里我们还需要另外去下载它,安卓4.0以上可以忽略,但在我们实际开发中,最低版本一般还是要求2.2或者2.3以上,这也是为了向下兼容ActionBar。这是ActionBarSherlock的下载地址:https://github.com/JakeWharton/ActionBarSherlock
2、既然下载好了所需要的文件,那么就将其导入项目吧,在这里只需要导入2个文件夹
(1)SlidingMenu里的library (2)ActionBarSherlock里的actionbarsherlock
引用这2个库文件:(注意点:不管是导入库还是自己建的项目,android-support-v4.jar的版本一定要一致,最好复制一份,集体覆盖一遍)
接着就可以开始进入开发工作了,这里的SlidingMenu你可以仅仅只作为一个View进入,直接将它的实现写在Activity。为了不使Activity那么的冗余,我这里借助Fragment来实现,这也更接近我们日常的开发环境,把Activity当做容器,上面装载着两个Fragment,一个是侧滑菜单SlideMenu,一个是主界面内容。
先来看下XML布局文件:
1、主Activity界面,里面装了一个FrameLayout布局,便于一会需要主界面布局和菜单布局来覆盖替换它。
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <FrameLayout 7 android:id="@+id/fl_main" 8 android:layout_width="fill_parent" 9 android:layout_height="fill_parent" 10 ></FrameLayout> 11 12 </RelativeLayout>
2、侧滑菜单布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/leftmenu" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@drawable/img_frame_background" 7 > 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical" > 13 14 <RelativeLayout 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_centerInParent="true" > 18 19 <ImageView 20 android:id="@+id/menuimage1" 21 android:layout_width="50dp" 22 android:layout_height="50dp" 23 android:layout_centerVertical="true" 24 android:layout_marginLeft="20dp" 25 android:layout_marginTop="20dp" 26 android:src="@drawable/img_1" /> 27 28 <TextView 29 android:id="@+id/menutext1" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_centerVertical="true" 33 android:layout_marginLeft="20dp" 34 android:layout_marginTop="20dp" 35 android:layout_toRightOf="@id/menuimage1" 36 android:text="菜单一" 37 android:textColor="@android:color/white" 38 android:textSize="20dp" /> 39 </RelativeLayout> 40 41 42 <RelativeLayout 43 android:layout_width="match_parent" 44 android:layout_height="wrap_content" 45 android:layout_centerInParent="true" > 46 47 <ImageView 48 android:id="@+id/menuimage2" 49 android:layout_width="50dp" 50 android:layout_height="50dp" 51 android:layout_centerVertical="true" 52 android:layout_marginLeft="20dp" 53 android:layout_marginTop="20dp" 54 android:src="@drawable/img_2" /> 55 56 <TextView 57 android:id="@+id/menutext2" 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:layout_centerVertical="true" 61 android:layout_marginLeft="20dp" 62 android:layout_marginTop="20dp" 63 android:layout_toRightOf="@id/menuimage2" 64 android:text="菜单二" 65 android:textColor="@android:color/white" 66 android:textSize="20dp" /> 67 </RelativeLayout> 68 69 <RelativeLayout 70 android:layout_width="match_parent" 71 android:layout_height="wrap_content" 72 android:layout_centerInParent="true" > 73 74 <ImageView 75 android:id="@+id/menuimage3" 76 android:layout_width="50dp" 77 android:layout_height="50dp" 78 android:layout_centerVertical="true" 79 android:layout_marginLeft="20dp" 80 android:layout_marginTop="20dp" 81 android:src="@drawable/img_3" /> 82 83 <TextView 84 android:id="@+id/menutext3" 85 android:layout_width="wrap_content" 86 android:layout_height="wrap_content" 87 android:layout_centerVertical="true" 88 android:layout_marginLeft="20dp" 89 android:layout_marginTop="20dp" 90 android:layout_toRightOf="@id/menuimage3" 91 android:text="菜单三" 92 android:textColor="@android:color/white" 93 android:textSize="20dp" /> 94 </RelativeLayout> 95 96 97 <RelativeLayout 98 android:layout_width="match_parent" 99 android:layout_height="wrap_content" 100 android:layout_centerInParent="true" > 101 102 <ImageView 103 android:id="@+id/menuimage4" 104 android:layout_width="50dp" 105 android:layout_height="50dp" 106 android:layout_centerVertical="true" 107 android:layout_marginLeft="20dp" 108 android:layout_marginTop="20dp" 109 android:src="@drawable/img_4" /> 110 111 <TextView 112 android:id="@+id/menutext4" 113 android:layout_width="wrap_content" 114 android:layout_height="wrap_content" 115 android:layout_centerVertical="true" 116 android:layout_marginLeft="20dp" 117 android:layout_marginTop="20dp" 118 android:layout_toRightOf="@id/menuimage4" 119 android:text="菜单四" 120 android:textColor="@android:color/white" 121 android:textSize="20dp" /> 122 </RelativeLayout> 123 124 125 <RelativeLayout 126 android:layout_width="match_parent" 127 android:layout_height="wrap_content" 128 android:layout_centerInParent="true" > 129 130 <ImageView 131 android:id="@+id/menuimage5" 132 android:layout_width="50dp" 133 android:layout_height="50dp" 134 android:layout_centerVertical="true" 135 android:layout_marginLeft="20dp" 136 android:layout_marginTop="20dp" 137 android:src="@drawable/img_5" /> 138 139 <TextView 140 android:id="@+id/menutext5" 141 android:layout_width="wrap_content" 142 android:layout_height="wrap_content" 143 android:layout_centerVertical="true" 144 android:layout_marginLeft="20dp" 145 android:layout_marginTop="20dp" 146 android:layout_toRightOf="@id/menuimage5" 147 android:text="菜单五" 148 android:textColor="@android:color/white" 149 android:textSize="20dp" /> 150 </RelativeLayout> 151 </LinearLayout> 152 153 </RelativeLayout>