[AndroidUI]使用DrawerLayout实现侧滑菜单

一、创建抽屉布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- The main content view -->

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

    <!-- The navigation view -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"     
        android:layout_gravity="start"
        android:background="#ffffcc"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" >
    </ListView>

</android.support.v4.widget.DrawerLayout>

[AndroidUI]使用DrawerLayout实现侧滑菜单_第1张图片


注意事项:

1、主内容视图一定要是DrawerLayout的第一个子视图

2、主内容视图宽度和高度匹配父视图,即“match_parent”
3、必须显示指定抽屉视图(如ListView)的 android:layout_gravity 属性

android:layout_gravity=“start”时,从左向右滑出菜单

android:layout_gravity=“end”  时,从右向左滑出菜单

不推荐使用 “left”和“right”

4、抽屉视图的宽度以dp为单位,请不要超过320dp(为了总能看到一些主内容视图)


二、初始化导航列表

package com.example.testdrawerlayout;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ContentFragment extends Fragment {
	
	private TextView textView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_content, container, false);
		textView = (TextView) view.findViewById(R.id.textView);
		
		String text = getArguments().getString("text");
		textView.setText(text);
		
		return view;
	}
	
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

</LinearLayout>

package com.example.testdrawerlayout;

import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.R.anim;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
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;

public class MainActivity extends Activity implements OnItemClickListener {

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ArrayList<String> menuLists;
	private ArrayAdapter<String> adapter;

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

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		menuLists = new ArrayList<String>();
		for (int i = 0; i < 50; i++)
			menuLists.add("极客学院0" + i);
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menuLists);
		mDrawerList.setAdapter(adapter);
		mDrawerList.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
		// 动态插入一个Fragment到FrameLayout当中
		Fragment contentFragment = new ContentFragment();
		Bundle args = new Bundle();
		args.putString("text", menuLists.get(position));
		contentFragment.setArguments(args);

		FragmentManager fm = getFragmentManager();
		fm.beginTransaction().replace(R.id.content_frame, contentFragment).commit();

		mDrawerLayout.closeDrawer(mDrawerList);
	}
}

[AndroidUI]使用DrawerLayout实现侧滑菜单_第2张图片


三、监听抽屉的打开关闭事件


四、点击图标开闭抽屉

你可能感兴趣的:(DrawerLayout,侧滑菜单)