標籤:
廢話不多說,直接上:
其實Google官方已經給出了一個 關於DrawerLayout 使用的例子,只是處於 國內不能訪問Google官網,看不到詳細文檔說明,所以在此 簡單記錄下 側滑 抽屜式菜單的使用說明
1.由於 DrawerLayout 需要 android.support.v4 包的支援,所以,你的libs 下面不要包含 這個包。
2.首先布局檔案如下
<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/frame_content" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <!-- the navigetion view --> <ListView android:id="@+id/drawer_list" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#9999cc" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" > </ListView></android.support.v4.widget.DrawerLayout>
注意:1.布局中的ListView 就是就是側滑菜單 版面配置容器,其中 android:layout_gravity 屬性必須賦值。 目的是側滑菜單的顯示位置,官方推薦使用“start”-----左側 "end"----右側
不推薦使用 “left”---左側,“right”---右側。
2.FrameLayout 顯示主內容部分。
其實到了這一步就已經實現了側滑菜單效果了,可以運行試試,只是現在的側滑菜單裡面沒有任何元素。
3.代碼編寫如下:
package com.example.drawlayout1;import android.os.Bundle;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.content.res.Configuration;import android.support.v4.app.ActionBarDrawerToggle;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;import android.view.LayoutInflater;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 String[] menuLists;private ArrayAdapter<String> adapter;private ActionBarDrawerToggle mDrawerToggle;private String titleString;private int iSelect = -1;private View heandrView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*get the application title*/titleString = (String) getTitle();heandrView = LayoutInflater.from(this).inflate(R.layout.home_menu_list_header, null);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);mDrawerList = (ListView) findViewById(R.id.drawer_list);mDrawerList.addHeaderView(heandrView);menuLists = getResources().getStringArray(R.array.menu_content);adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, menuLists);mDrawerList.setAdapter(adapter);mDrawerList.setOnItemClickListener(this);/*set the shadow for drawer at start(left) or end(right)*/mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);/*show the home icon*/getActionBar().setDisplayHomeAsUpEnabled(true);/*make sure the home icon enable click*/getActionBar().setHomeButtonEnabled(true);/*set the application ActionBar title changes*/mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) {@Overridepublic void onDrawerOpened(View drawerView) {super.onDrawerOpened(drawerView);getActionBar().setTitle(R.string.please);}@Overridepublic void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);if (-1 == iSelect) {getActionBar().setTitle(titleString);} else {getActionBar().setTitle(menuLists[iSelect-1]);}}};/*set the DrawerLayout Listener*/mDrawerLayout.setDrawerListener(mDrawerToggle);}@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {/*make sure the home icon enable click and display the DrawerLayout*/if (mDrawerToggle.onOptionsItemSelected(item)){return true;}return super.onOptionsItemSelected(item);}@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Fragment contentFragment = new ContentFragment();Bundle args = new Bundle();iSelect = arg2;if (0 == iSelect){iSelect = 1;}args.putString("text", menuLists[iSelect-1]);contentFragment.setArguments(args);FragmentManager fm = getFragmentManager();fm.beginTransaction().replace(R.id.frame_content, contentFragment).commit();mDrawerLayout.closeDrawer(mDrawerList);}/** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */@Overrideprotected void onPostCreate(Bundle savedInstanceState) {super.onPostCreate(savedInstanceState);// Sync the toggle state after onRestoreInstanceState has occurred.mDrawerToggle.syncState();}@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);// Pass any configuration change to the drawer togglsmDrawerToggle.onConfigurationChanged(newConfig);}}代碼裡邊有註解,就不一一解釋了。
@Overridepublic boolean onOptionsItemSelected(MenuItem item) {/*make sure the home icon enable click and display the DrawerLayout*/if (mDrawerToggle.onOptionsItemSelected(item)){return true;}return super.onOptionsItemSelected(item);}
這段代碼的目的:點擊ActionBar導覽列上的Home 按鍵彈出和關閉 DrawerLayout 側滑菜單。
/** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */@Overrideprotected void onPostCreate(Bundle savedInstanceState) {super.onPostCreate(savedInstanceState);// Sync the toggle state after onRestoreInstanceState has occurred.mDrawerToggle.syncState();}@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);// Pass any configuration change to the drawer togglsmDrawerToggle.onConfigurationChanged(newConfig);}
這段代碼的目的:Google官方極力推薦重寫兩個方法,第一個方法 實現ActionBar 的Home鍵和 DrawerLayout 抽屜菜單關聯,並且ActionBar的Home 有動畫效果。
第二個方法 是在橫屏的時候調用。儲存狀態。
mDrawerList.setOnItemClickListener(this);
這段代碼是添加DrawerLayout 抽屜菜單 裡面元素的點擊事件,代碼中實現點擊不同元素,替換不同FragMent。
/*set the application ActionBar title changes*/mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) {@Overridepublic void onDrawerOpened(View drawerView) {super.onDrawerOpened(drawerView);getActionBar().setTitle(R.string.please);}@Overridepublic void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);if (-1 == iSelect) {getActionBar().setTitle(titleString);} else {getActionBar().setTitle(menuLists[iSelect-1]);}}};/*set the DrawerLayout Listener*/mDrawerLayout.setDrawerListener(mDrawerToggle);
這段代碼是 添加側滑 菜單 DrawerLayout 的開啟關閉監聽事件,在開啟關閉方法中重寫 自己需要實現的效果。
最後附上 整個工程源碼:源碼工程
google 原生態 抽屜式側滑菜單 Android DrawerLayout 布局的使用介紹