牛骨文教育服务平台(让学习变的简单)

项目要实现类似于网易新闻客户端的侧滑拉出菜单的功能,搜了好些资料,有下面的三种方法:

1)自定义viewgroup

2)导入开源项目slidingmenu_library

3)采用V4包的组件DrawerLayout

第三种方法是最方便快捷的了,虽然第三种方法不能很好地支持低版本安卓手机,但是因为我们也没有这种需求,所以最后我还是决定采用DrawerLayout。不过第一种方法是最好的(虽然从开发效率上来说不是),有空自己还得要学一下自己造轮子。

DrawerLayout的xml:

<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"
    tools:context=".MainActivity" >

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

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

            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="open" />
        </FrameLayout>

        <LinearLayout
            android:id="@+id/left_layout"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FFB5C5"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="20sp" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="20sp" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textSize="20sp" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4"
                android:textSize="20sp" >
            </TextView>
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

其中包裹了2个或者以上的view,需要注意的是第一个view是主页面的view,后面的才是侧滑出页面的view。主页面view的宽高要写成

android:layout_width="match_parent"
android:layout_height="match_parent"

因为在没有侧滑的时候是要包裹父view窗体的。

而在侧滑出的页面的view要设置layout_gravity的值指明是向左滑出还是向右滑出。

下面的Activity,比较简单:

package com.example.drawerlayout;

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 MainActivity extends Activity {
	private DrawerLayout mDrawerLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		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);
			}
		});
	}

}

直接调用openDrawer就可以拉开侧滑页面了。需要注意的是后面也要写明侧滑方向(LEFT),并且与前面xml标明的方向要一致,不然可能会出错。

DrawerLayout还有很多很好用的API,我还在研究中所以就不贴出来了。