Android抽屜導航Navigation Drawer實例解析
我們重點來研究一下Android抽屜導航 NavigationDrawer。先來感性認識一下這種效果吧:

看了很多應用,覺得這種側(cè)滑的抽屜效果的菜單很好。不用切換到另一個頁面,也不用去按菜單的硬件按鈕,直接在界面上一個按鈕點擊,菜單就滑出來,而且感覺能放很多東西。
最簡單就是用官方的抽屜導航 NavigationDrawerLayout 來實現(xiàn)。DrawerLayout這個類是在Support Library里的,需要加上android-support-v4.jar這個包。然后程序中用時在前面導入import android.support.v4.widget.DrawerLayout;
如果找不到這個類,首先用SDK Manager更新一下Android Support Library,然后在Android SDK\extras\android\support\v4路徑下找到android-support-v4.jar,復制到項目的libs路徑,將其Add to Build Path.
當你新建一個 Android 項目的時候,你可以選擇使用 Navigation Drawer:

我們來簡要看看代碼,首先是 NavigationDrawerFragment.java 這個類,加載了哪些布局文件。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 給抽屜ListView找到對應的XML布局
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
// 給抽屜ListView綁定點擊監(jiān)聽器,點擊時,選中點擊的項
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
// 給抽屜ListView綁定一個適配器
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),
}));
//mDrawerListView.setAdapter(new DrawerAdapter(getActivity()));
// 設置抽屜ListView以顯示某一選中項的形態(tài)出現(xiàn)。
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
// 將處理后的抽屜ListView返回
return mDrawerListView;
}
NavigationDrawer 主要是一個 ListView,這個 ListView 使用了 fragment_navigation_drawer.xml:
<ListView 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:background="@color/image_bg_green" android:choiceMode="singleChoice" android:divider="@color/image_bg_lightgreen" android:dividerHeight="1dp" tools:context="net.nowamagic.magicapp_v7.NavigationDrawerFragment" />
這個 ListView 就是抽屜導航直觀上看到的那個 ListView。同時 ListView 里面每個格子都由一個相對布局填充,其 XML 為 fragment_main.xml:
<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: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="net.nowamagic.magicapp_v7.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
新建一個基于 NavigationDrawer 的項目,大概效果如下:

以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android 自定義View實現(xiàn)抽屜效果
- Android自定義控件仿QQ抽屜效果
- Android DrawerLayout實現(xiàn)抽屜效果實例代碼
- Android 抽屜效果的導航菜單實現(xiàn)代碼實例
- Android實現(xiàn)自定義滑動式抽屜菜單效果
- Android App中DrawerLayout抽屜效果的菜單編寫實例
- Android SlidingDrawer 抽屜效果的實現(xiàn)
- Android實現(xiàn)3種側(cè)滑效果(仿qq側(cè)滑、抽屜側(cè)滑、普通側(cè)滑)
- Android實現(xiàn)右邊抽屜Drawerlayout效果
- Android組件之DrawerLayout實現(xiàn)抽屜菜單
- Android提高之多方向抽屜實現(xiàn)方法
- Android編程實現(xiàn)抽屜效果的方法詳解
相關(guān)文章
Android編程實現(xiàn)禁止狀態(tài)欄下拉的方法詳解
這篇文章主要介紹了Android編程實現(xiàn)禁止狀態(tài)欄下拉的方法,結(jié)合實例形式詳細分析了Android狀態(tài)欄操作相關(guān)的函數(shù)、屬性調(diào)用及權(quán)限控制設置技巧,需要的朋友可以參考下2017-08-08
Android項目中使用Eclipse導出jar文件的操作方法
文章講述了如何使用Eclipse將Android項目打包成jar文件,并詳細解答了在打包過程中遇到的問題,如如何處理依賴的jar或library,以及如何解決在其他項目中引用時出現(xiàn)的NoClassDefFoundError錯誤2025-02-02
Android相冊效果(使用C#和Java分別實現(xiàn))
這篇文章主要介紹了Android相冊效果(使用C#和Java分別實現(xiàn)),原來C#也可以開發(fā)APP,小編第一次見了~感覺不錯,因為小編暫時不喜歡Java,所以,需要的朋友可以參考下2015-06-06
Android利用CountDownTimer實現(xiàn)點擊獲取驗證碼倒計時效果
這篇文章主要為大家詳細介紹了Android利用CountDownTimer實現(xiàn)點擊獲取驗證碼倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
RecyclerView嵌套RecyclerView滑動卡頓的解決方法
這篇文章主要為大家詳細介紹了RecyclerView嵌套RecyclerView滑動卡頓的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

