編輯:關於 Android 程式設計
瀏覽了很多應用後發現,這種側滑抽屜式的菜單體驗非常好。

它不必切換到另一個頁面,也不用去按菜單的物理按鍵,只需在界面上一個操作,菜單便滑了出來,而且能容納相當多的內容。
關於實現方式,搜索後主要有兩種:
1. 使用 SlidingDrawer,但這個類自 API 17 起已被官方標記為 Deprecated,不建議再使用;
2. 使用 DrawerLayout,這是目前官方推薦的方案,配套開發指南可查閱官方文檔。
庫的引用
DrawerLayout 位於 Support Library 中,需要引入 android-support-v4.jar 這個包,並在代碼中導入:
import android.support.v4.widget.DrawerLayout;
如果找不到該類,可先用 SDK Manager 更新 Android Support Library,再到 Android SDK\extras\android\support\v4 路徑下找到 jar 包,複制到項目的 libs 目錄並 Add to Build Path 即可。
代碼一
布局:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" 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" /> <!-- 抽屜內容 --> <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 的第一個子元素是主要內容(抽屜打開時顯示的布局),第二個子元素才是抽屜內容。這裡抽屜採用了一個 ListView。
主要 Activity(整理自官方示例):
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); initListView(); 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) { public void onDrawerClosed(View view) { invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); getActionBar().setDisplayHomeAsUpEnabled(true); } private void initListView() { mDrawerList = (ListView) findViewById(R.id.left_drawer); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mPlanetTitles)); mDrawerList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mDrawerList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } }); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); }
需要注意的是,這裡用到了 Level 11 的 API,因此 minSdkVersion 不能設得太低。相關圖片資源可在 Android 官網示例處下載。
程序運行後的效果:
抽屜打開前:

抽屜打開後:

代碼二
仔細研究 DrawerLayout 的類後發現,其實它有現成的方法可以直接調用,不必像上面那樣麻煩。只要自定義一個按鈕控制抽屜的開合即可。
布局:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".DrawerActivity" > <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="match_parent" android:layout_height="wrap_content" android:text="open" /> </FrameLayout> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
主要代碼:
package com.example.hellodrawer; 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 DrawerActivity extends Activity { private DrawerLayout mDrawerLayout = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawer); 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); } }); } }
可以看到,用 DrawerLayout 的 openDrawer 方法,只需一行就能實現抽屜的展開,非常簡潔。以上就是抽屜式導航菜單的完整實現,希望能對大家的學習有所幫助。
Android 讀取 assets 與 raw 資源文件的完整做法
把配置文件、初始數據這類資源打包進 APK,是很常見的做法。Android 提供兩個位置可以放:assets 目錄與 res/raw 目錄。兩者有一個共同前提——只能讀,
Android View 工作原理:從 setContentView 到 DecorView 的追蹤
從 setContentView 開始 Activity 的 setContentView 會呼叫 getWindow().setContentView。Window 是
Android學習筆記(八):打造友善的進度對話框體驗
為什麼需要進度對話框?不知道你有沒有遇過這種情況:點擊某個功能後,畫面卻卡在那裡一動也不動,讓人搞不清楚程式到底是當機了還是正在忙。這時候,一個貼心的「Please Wa
Android TextView 垂直滾動設定:讓長文本可捲動顯示
讓 TextView 可以垂直滾動當 TextView 內容過長時,可以透過 XML 屬性讓它顯示垂直滾動條。核心設定包括 android:scrollbars=vert