Android抽屜導航Navigation Drawer執行個體解析_Android

來源:互聯網
上載者:User

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


看了很多應用,覺得這種側滑的抽屜效果的菜單很好。不用切換到另一個頁面,也不用去按菜單的硬體按鈕,直接在介面上一個按鈕點擊,菜單就滑出來,而且感覺能放很多東西。

最簡單就是用官方的抽屜導航 NavigationDrawerLayout 來實現。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 這個類,載入了哪些布局檔案。

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {  // 給抽屜ListView找到對應的XML布局  mDrawerListView = (ListView) inflater.inflate(      R.layout.fragment_navigation_drawer, container, false);  // 給抽屜ListView綁定點擊監聽器,點擊時,選中點擊的項  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以顯示某一選中項的形態出現。  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 的項目,大概效果如下:

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.