一直看别人应用里面有一个侧滑菜单,感觉好酷毙,但是自己也不会写,那个心情叫做难过了,恨自己不会。我也百度了一段时间,只恨自己的水平不高,看不懂代码,所以就找了许多的例子,但是还看不懂,终于一次偶然的机会,我找到了,是的,找到了。这个代码就很简单了,因为它提供了别人写的一个类,就是布局类,直接可以在XML中使用,然后就可以直接运行了,至于要加什么东西,就随自己的心意了。
好了,不多说了,现在开始讲实现的过程,真的很简单,句相当于提供了一个已经有的空间,你所要做的就是往里面填代码,然后就可以运行了。(当时看别人牛逼,是不是他用的也是这个类啊,其实大家也差不多嘛)
效果图给大家看一下:
正如效果图所示,菜单在左边,向右滑动时,主界面向右移动,菜单慢慢出来,比Android自己提供的那种菜单要炫酷多了。
好了,说了那个类到底是哪个呢?
SlideHolder,它是FrameLayout的子类(它不是Android提供的,是别人自己写的),也就是说,这个是在XML布局文件中使用的,如下面的代码,它是MainActivity的布局代码:
<span style="font-size:18px;"><com.example.slidemenu_test.SlideHolder xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ScrollView
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#000000"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="菜单1"
android:textColor="#ffffff"
android:textSize="25sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="菜单2"
android:textColor="#ffffff"
android:textSize="25sp"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/btn_icon"
android:background="#0000"
/>
</LinearLayout>
</LinearLayout>
</com.example.slidemenu_test.SlideHolder></span>
接下来我对这个代码进行说明,在新建一个Android项目的时候,Eclipse会自动生成activity_main.xml代码,它是MainActivity的布局文件,我们需要把activity_main.xml里面的内容换成上面所示,然后就可以直接运行了。在上面的代码中,首先是ScrollView,也就是菜单,我们往这个布局里面加入自己想要的内容,我就随便加了一点,做为测试嘛,接下来是一个布局LinearLayout,这个就是主界面了,也就是不滑动的时候显示的界面,在这里面,我加了一个按钮来控制菜单的弹出,当然也可以不设置,可以直接运行的,但是许多应用都设置了,我也就设置一下,我添加了一个ImageButton,在MainActivity里面对它进行了设置,下面我们来看MainActivity的代码(简单易懂):
package com.example.slidemenu_test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity {
private ImageButton mBtn;
private SlideHolder mSlideHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn = (ImageButton) findViewById(R.id.imageButton);
mSlideHolder = (SlideHolder) findViewById(R.id.holder);
mBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSlideHolder.toggle();
}
});
}
}
在这个代码里,有一个ImageButton对象和一个SlideHolder对象,给ImageButton添加事件,点击一下就弹出菜单,弹出菜单也比较简单,就一行代码mSlideHolder.toggle(); 函数toggle是SlideHolder中的方法大家想了解更多的我把源码下载下来自己多了解吧。
好了,至此,一个侧滑菜单就出来了(站在别人的肩膀上,我能看的更远^_^)
源码:点此下载