概述
最近流行 左側(cè)抽屜式的導(dǎo)航條菜單浅妆,知乎缀壤,360,QQ都使用了這樣的導(dǎo)航菜單典勇,我們也了解下:
Android Design 的流行趨勢(shì):Navigation Drawer 導(dǎo)航抽屜 參考這篇文章:http://www.geekpark.net/topics/183724
效果圖:
特點(diǎn)
1.標(biāo)題欄(或者actionBar) 做的有個(gè) 菜單圖標(biāo)按鈕(三條線或者其他)桅锄。一般這樣的標(biāo)題欄左側(cè)和右側(cè)都會(huì)有圖標(biāo)按鈕琉雳。如圖1所示。
2.點(diǎn)擊圖標(biāo)按鈕 從左側(cè)向右 慢慢退出一個(gè) 菜單視圖(View)友瘤,遮蓋在 內(nèi)容頁(yè)(首頁(yè))的視圖上翠肘,同時(shí),產(chǎn)生遮蓋層辫秧。如圖2所示束倍。
實(shí)
官方示例
參考自谷歌開(kāi)發(fā)者網(wǎng)站的示例,在這個(gè)頁(yè)面可以下載到示例盟戏。http://developer.android.com/training/implementing-navigation/nav-drawer.html
引用類(lèi)庫(kù)
需要android-support-v4.jar
主要控件
谷歌提供的抽屜控件: android.support.v4.widget.DrawerLayout
參考這片文章的解釋?zhuān)?a target="_blank" rel="nofollow">http://blog.csdn.net/xiahao86/article/details/8995827
具體實(shí)現(xiàn)
首頁(yè)(比如叫:MainActivity)內(nèi)容布局肌幽,寫(xiě)一個(gè) android.support.v4.widget.DrawerLayout,它需要包含兩個(gè)內(nèi)容視圖元素抓半,第一個(gè)視圖元素是 主顯示內(nèi)容頁(yè),第二個(gè)是要抽屜彈出的視圖格嘁。
<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" >
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view.
-->
<zyf.demo.navigationdrawer.NavigationMenu
android:id="@+id/navigation_menu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" >
</zyf.demo.navigationdrawer.NavigationMenu>
</android.support.v4.widget.DrawerLayout>
我在這里寫(xiě)了個(gè)自定義控件 “ zyf.demo.navigationdrawer.NavigationMenu " 笛求,
注意它的 android:layout_gravity="start" ,是 start 不是left糕簿。
MainActivity需要 為DrawerLayout 注冊(cè)一個(gè)回調(diào)事件接口ActionBarDrawerToggle 探入,這個(gè)事件的實(shí)現(xiàn)者監(jiān)聽(tīng)器會(huì)獲得 抽屜彈出(onDrawerOpened)和關(guān)閉(onDrawerClosed)的事件。
MainActivity 的代碼
package zyf.demo.navigationdrawer;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private NavigationMenu mNavigationMenu;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲得抽屜控件
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 獲得菜單控件
mNavigationMenu = (NavigationMenu) findViewById(R.id.navigation_menu);
mNavigationMenu.attacthDrawer(mDrawerLayout);
// enable ActionBar app icon to behave as action to toggle nav
// drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
// 使actionbar 的logo圖標(biāo)透明不可見(jiàn)
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(
android.R.color.transparent)));
// 注冊(cè)導(dǎo)航菜單抽屜 的彈出和關(guān)閉事件
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_notification_content, /*
* nav drawer image to replace 'Up'
* caret
*/
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
// 當(dāng)導(dǎo)航菜單抽屜 關(guān)閉后
public void onDrawerClosed(View view) {
// 顯示當(dāng)前內(nèi)容頁(yè)的標(biāo)題
getActionBar().setTitle(getTitle());
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
// 當(dāng)導(dǎo)航菜單抽屜 打開(kāi)后
public void onDrawerOpened(View drawerView) {
// 顯示導(dǎo)航菜單的標(biāo)題
getActionBar().setTitle(mNavigationMenu.getTitle());
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
// 顯示首頁(yè)的內(nèi)容
showFragment(new HomeFragment());
// 當(dāng)更換主頁(yè)內(nèi)的 子頁(yè)面(fragment)時(shí),隱藏導(dǎo)航菜單
mNavigationMenu.hide();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
// 當(dāng)彈出導(dǎo)航菜單時(shí)懂诗,使 actionbar的擴(kuò)展按鈕不可見(jiàn)
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mNavigationMenu);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
case R.id.action_websearch:
Toast.makeText(this, "你點(diǎn)擊了搜索按鈕", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showFragment(Fragment fragment) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
@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);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
下面給出我寫(xiě)的自定義控件的實(shí)現(xiàn):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#FFFFFF"/>
</RelativeLayout>
代碼
package zyf.demo.navigationdrawer;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class NavigationMenu extends RelativeLayout {
LayoutInflater mLayoutInflater;
ListView mlistView1;
String[] menuItemsDataSource;
private DrawerLayout mDrawerLayout;// 關(guān)聯(lián)到的抽屜控件
public NavigationMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initLayout(context);
}
public NavigationMenu(Context context, AttributeSet attrs) {
super(context, attrs);
initLayout(context);
}
public NavigationMenu(Context context) {
super(context);
initLayout(context);
}
private void initLayout(Context context) {
mLayoutInflater = LayoutInflater.from(context);
View contentView = mLayoutInflater.inflate(R.layout.navigation_menu,
null);
RelativeLayout.LayoutParams lp = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.addView(contentView, lp);
mlistView1 = (ListView) contentView.findViewById(R.id.listView1);
menuItemsDataSource = getResources().getStringArray(
R.array.navigation_menu_items_array);
mlistView1.setAdapter(new ArrayAdapter<String>(context,
R.layout.navaigation_menu_list_view_item, menuItemsDataSource));
mlistView1.setOnItemClickListener(new DrawerItemClickListener());
}
/**
* 包含 的 listView的點(diǎn)擊事件
* @author yunfei
*
*/
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// selectItem(position);
mlistView1.setItemChecked(position, true);
hide();
Toast.makeText(getContext(), "你選擇了" + position, 0).show();
}
}
public CharSequence getTitle() {
return "導(dǎo)航菜單";
}
/**
* 關(guān)聯(lián)到 drawerLayout
* @param drawerLayout
*/
public void attacthDrawer(DrawerLayout drawerLayout) {
mDrawerLayout = drawerLayout;
}
/**
* 隱藏
*/
public void hide() {
if (mDrawerLayout != null)
mDrawerLayout.closeDrawer(NavigationMenu.this);
}
/**
* 出現(xiàn)
*/
public void show() {
if (mDrawerLayout != null)
mDrawerLayout.openDrawer(NavigationMenu.this);
}
}
代碼下載:
http://yunpan.cn/cggiDkFNCp2Jw 訪問(wèn)密碼 c3ed
參考:
http://blog.csdn.net/xiahao86/article/details/8995827
http://developer.android.com/training/implementing-navigation/nav-drawer.html
http://www.geekpark.net/topics/183724