Android 学习之drawerlayout写侧滑菜单

简介: 版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/49467317 Android 学习之Drawerlayout写侧滑菜单我们经常利用SlidingMenu来实现侧滑菜单,谷歌推出的DrawerLayout也可以实现侧滑菜单,完全可以替代SlidingMenu,如下图所示都是用SlidingMenu开发的实例。
版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/49467317

Android 学习之Drawerlayout写侧滑菜单

我们经常利用SlidingMenu来实现侧滑菜单,谷歌推出的DrawerLayout也可以实现侧滑菜单,完全可以替代SlidingMenu,如下图所示都是用SlidingMenu开发的实例。

1、项目结构

                                                   
JAVA文件中首先是一个主界面的Activity-——MainActivity,然后是一个继承与Fragment的类——ContentFragment,
布局文件中,activity_main对应于主布局文件,fragment_content对应于Fragment的布局。

2、MainActivity.java

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.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener {

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private LinearLayout mlayout;
	private ArrayList<String> menuLists;
	private ArrayAdapter<String> adapter;
	private ActionBarDrawerToggle mDrawerToggle;
	private String mTitle;
    private ImageView imageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mTitle = (String) getTitle();
		imageView = (ImageView) findViewById(R.id.image);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mlayout = (LinearLayout) findViewById(R.id.mlayout);


		menuLists = new ArrayList<String>();
		for (int i = 0; i < 5; i++)
			menuLists.add("菜单0" + i);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, menuLists);
		mDrawerList.setAdapter(adapter);
		mDrawerList.setOnItemClickListener(this);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {
			@Override
			public void onDrawerOpened(View drawerView) {
				super.onDrawerOpened(drawerView);
				getActionBar().setTitle("请选择");
				invalidateOptionsMenu(); // Call onPrepareOptionsMenu()
			}

			@Override
			public void onDrawerClosed(View drawerView) {
				super.onDrawerClosed(drawerView);
				getActionBar().setTitle(mTitle);
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		//开启ActionBar的App Icon功能
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mlayout);
		menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//将ActionBar上的图标与Drawer结合起来
		if (mDrawerToggle.onOptionsItemSelected(item)){
			return true;
		}
		switch (item.getItemId()) {
		case R.id.action_websearch:
			Intent intent = new Intent();
			intent.setAction("android.intent.action.VIEW");
			Uri uri = Uri.parse("http://www.csdn.net");
			intent.setData(uri);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		//需要将ActionDrawerToggle与DrawerLayout的状态同步
		//将ActionBarDrawerToggle中的drawer图标,设置为ActionBar中的Home-Button的Icon
		mDrawerToggle.syncState();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// 在FrameLayout中动态插入一个Fragment
		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(mlayout);
	}

}


3、ContentFragment.java

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


4、activity_main.xml

<span style="font-weight: normal;"><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 -->
    <LinearLayout
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:id="@+id/mlayout"
        android:background="#333fff"
        android:layout_height="match_parent"
        android:orientation="vertical">
<LinearLayout
    android:layout_width="240dp"
    android:layout_height="200dp"
    android:background="@drawable/bg"
    android:layout_gravity="start">
    <ImageView
        android:id="@+id/image"
       android:src="@drawable/tu"
        android:layout_gravity="start"
        android:layout_width="50dp"
        android:layout_height="50dp" />
</LinearLayout>
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffcc"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" >
    </ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout></span>


5、fragment_content.xml

<span style="font-weight: normal;"><?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></span>


6、menu(main.xml)

<span style="font-weight: normal;"><menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_websearch"
        android:icon="@drawable/action_search"
        android:showAsAction="ifRoom|withText"
        android:title="webSearch"/>

</menu></span>

7、效果图
                                                                            




源码下载地址点此下载
相关文章
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
101 0
|
3月前
|
安全 Linux Android开发
Android安全启动学习(一):AVB校验是什么?
Android安全启动学习(一):AVB校验是什么?
99 0
|
3月前
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
124 0
|
25天前
|
Android开发
Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果
Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果
10 0
|
3月前
|
开发框架 物联网 数据库
89个android学习样例源码
89个android学习样例源码
76 0
|
3月前
|
Java Android开发 C++
2023安卓逆向 -- JNI学习(从开发到反编译)
2023安卓逆向 -- JNI学习(从开发到反编译)
21 0
|
3月前
|
Android开发
Android源码学习(五):AVB2.0-libavb库介绍2
Android源码学习(五):AVB2.0-libavb库介绍2
105 0
|
3月前
|
安全 算法 Android开发
Android安全启动学习(五):Android Verified Boot 2.0
Android安全启动学习(五):Android Verified Boot 2.0
223 0
|
3月前
|
存储 安全 Android开发
Android安全启动学习(三):AVB校验的内容、怎么校验、AVB的作用
Android安全启动学习(三):AVB校验的内容、怎么校验、AVB的作用
149 0
|
3月前
|
存储 缓存 安全
Android安全启动学习(二):android镜像有什么?
Android安全启动学习(二):android镜像有什么?
82 0