如圖所示,抽屜效果的導航菜單不用切換到另外一個頁面,也不用去按菜單的硬件按鈕,直接在界面左上角的一個按鈕點擊,菜單就滑出來,並且感受能放不少東西html
方式1.用SlidingDrawer:java
http://developer.android.com/reference/android/widget/SlidingDrawer.htmlandroid
可是不知道爲何這個類官方不建議再繼續用了:app
Deprecated since API level 17eclipse
方式2.用DrawerLayout:ide
http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html工具
Guide在這裏:佈局
http://developer.android.com/training/implementing-navigation/nav-drawer.html開發工具
代碼:android.support.v4.widget.DrawerLayout 實現抽屜效果的導航菜單ui
<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" > <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" > <!-- 主要內容的視圖--> <!-- main content must be the first element of DrawerLayout because it will be drawn first and drawer must be on top of it --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 導航菜單 --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout></RelativeLayout>
DrawerLayout的第一個子元素是主要內容,即抽屜沒有打開時顯示的佈局。這裏採用了一個FrameLayout,裏面什麼也沒放。
DrawerLayout的第二個子元素是抽屜中的內容,即抽屜佈局,這裏採用了一個ListView。
package com.example.hellodrawer; import android.os.Bundle; import android.app.Activity; import android.content.res.Configuration; 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; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; public class HelloDrawerActivity extends Activity { private String[] mPlanetTitles; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; private ListView mDrawerList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello_drawer); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // init the ListView and Adapter, nothing new initListView(); // set a custom shadow that overlays the main content when the drawer // opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close){ /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); // enable ActionBar app icon to behave as action to toggle nav drawer getActionBar().setDisplayHomeAsUpEnabled(true); // getActionBar().setHomeButtonEnabled(true); // Note: getActionBar() Added in API level 11 } private void initListView() { mDrawerList = (ListView) findViewById(R.id.left_drawer); mPlanetTitles = getResources().getStringArray(R.array.planets_array); // Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item, mPlanetTitles)); // Set the list's click listener mDrawerList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { // Highlight the selected item, update the title, and close the // drawer mDrawerList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } }); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle your other action bar items... return super.onOptionsItemSelected(item); } }
比較糾結的是用了Level 11的一個API,(Android API 11對應的是android 3.0)這樣minSdkVersion就有限制,android版本不能過低。
圖片資源Android官網示例處提供下載了。
程序運行後效果以下:
抽屜打開前:
抽屜打開後:
DrawerLayout
這個類是在Support Library裏的,須要加上android-support-v4.jar這個包。
而後程序中用時在前面導入import android.support.v4.widget.DrawerLayout;
若是找不到這個類,首先用SDK Manager更新一下Android Support Library(若是您採用的開發工具是android studio而非eclipse,則應該安裝Android Support Repository),而後在Android SDK\extras\android\support\v4路徑下找到android-support-v4.jar,複製到項目的libs路徑,將其Add to Build Path.
關於Android Support Library 詳情可閱讀:http://blog.csdn.net/crazybigfish/article/details/18554201