http://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
R.menu.main
android:showAsAction="ifRoom|withText"
如果有空间的话,就实现图标和文字。
-->
<item
android:id="@+id/action_websearch"
android:icon="@drawable/action_search"
android:showAsAction="ifRoom|withText"
android:title="@string/action_websearch"/>
</menu>
 <resources>

     <string name="app_name">左侧菜单</string>

     <string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array> <string name="drawer_open">打开</string>
<string name="drawer_close">关闭</string>
<string name="action_websearch">WebSearch</string>
<string name="app_not_available">Sorry, there\'s no web browser available</string> </resources>
 <!--
【 注意事项 】
1、主内容视图一定要是DrawerLayout的第一个子视图。
2、主要内容视图宽度和高度匹配父视图,即“match_parent”。
3、必须显示指定抽屉视图(如ListView)的android:layout_gravity属性
android:layout_gravity="start",从左向右滑出菜单;
android:layout_gravity="end", 从右向左滑出菜单;
不推荐使用"left"和"right"。
4、抽屉视图的宽度以dp为单位,请不要超过320dp(为了总能看到一些主内容视图)
--> <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent
for both width and height to consume the full space available. -->
<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.
-->
<!-- 在这里面动态插入Fragment -->
<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.
--> <!--
android:layout_gravity="start" 从左向右滑动
android:layout_gravity="end" 从右向左滑动
android:choiceMode="singleChoice" 单选模式
-->
<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>
 package com.example.android.navigationdrawerexample;

 import java.util.Locale;

 import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList; /* 1、mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener);
2、ActionBarDrawerToggle是DrawerLayout.DrawerListener的具体实现类;
1)改变android.R.id.home图标(构造方法)。
2)Drawer拉出、隐藏,带有android.R.id.home动画效果(syncState())。
3)监听Drawer拉出、隐藏事件。
3、覆写ActionBarDrawerToggle的onDrawerOpened()和onDrawerClosed()以监听抽屉拉出或隐藏事件。
*/
private ActionBarDrawerToggle mDrawerToggle; //private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTitle = getTitle();
// ListView列表数据
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer); // set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer
// 开启ActionBar上APP ICON的功能
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true); /**
* 第4个和第5个参数:打开和关闭的描述资源
*/
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
/** 覆写他的两个方法 */
@Override /** 当被关闭时,执行这个方法 */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
@Override /** 当被打开时,执行这个方法 */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("请选择");
// 重绘菜单项的ActionBar。
// 当调用此方法的时候,它会自动调用一个方法:Call onPrepareOptionsMenu(所以要重写)。
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) {
selectItem(0);
}
} @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) {
// 根据打开和关闭来实现右上角的图片显示。
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); // 获取打开状态
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// 将ActionBar上的图标与Drawer结合起来。
// 点击左上角的图标,打开和关闭抽屉。
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_websearch: // 右上角查询按钮
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri uri = Uri.parse("http://www.baidu.com");
intent.setData(uri);
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available,
Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
} /* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
} private void selectItem(int position) {
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
// 传入参数
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit(); // update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} @Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
} /**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/ @Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// 需要将ActionDrawerToggle与DrawerLayout的状态同步。
// 将ActionBarDrawerToggle中的drawer图标,设置为ActionBar中的Home-Button的ICON。
mDrawerToggle.syncState(); // 实现同步即可。
} @Override // 当屏幕旋转的时候调用。实现重新配置。
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
} /**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number"; public PlanetFragment() {
// Empty constructor required for fragment subclasses
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet,
container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources()
.getStringArray(R.array.planets_array)[i]; int imageId = getResources().getIdentifier(
planet.toLowerCase(Locale.getDefault()), "drawable",
getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image))
.setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}

Drawer Layout的更多相关文章

  1. 【原创+译文】官方文档中声明的如何创建抽屉导航栏(Navigation Drawer)

    如需转载请注明出处:http://www.cnblogs.com/ghylzwsb/p/5831759.html 创建一个抽屉导航栏 抽屉式导航栏是显示在屏幕的左边缘,它是应用程序的主导航选项面板.它 ...

  2. Android UI开发第三十二篇——Creating a Navigation Drawer

    Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...

  3. Navigation Drawer介绍

    在2013 google IO当天,Android团的更新了Support库,新版本(V13)的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Nav ...

  4. Creating a Navigation Drawer 创建一个导航侧边栏

    The navigation drawer is a panel that displays the app’s main navigation options on the left edge of ...

  5. Android官方终于支持 Navigation Drawer(导航抽屉)模式

    在2013 google IO当天,Android团的更新了Support库,新版本(V13)的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Nav ...

  6. Android Navigation Drawer(导航抽屉)

    Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Navigation ...

  7. Android设计和开发系列第二篇:Navigation Drawer(Develop)

    Creating a Navigation Drawer THIS LESSON TEACHES YOU TO: Create a Drawer Layout Initialize the Drawe ...

  8. ANDROID – TOOLBAR 上的 NAVIGATION DRAWER(转)

    在 Material Design 釋出後,Google 也開始陸續更新了 Google app 的介面,讓大家有個範例可以看.而過去大力推動的 actionbar 自然而然也成了眾開發者觀注的部份: ...

  9. github上一款特别的侧滑

    知识分享: 首先看图,我只是大自然的搬运工,想实现这种特效的请点击连接下载github地址忘掉了,....http://download.csdn.net/detail/lj419855402/860 ...

随机推荐

  1. [转]Ubuntu 12.04 安装屏保

    From:http://www.howtogeek.com/114027/how-to-add-screensavers-to-ubuntu-12.04/ How to Add Screensaver ...

  2. @QueryParam和@PathParam比较

    来源:http://jackyrong.iteye.com/blog/1128364 1 先来看@queryparam Path("/users") public class Us ...

  3. 找啊找啊找GF

    P1013 找啊找啊找GF 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 MM七夕模拟赛 描述 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手, ...

  4. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  5. 两个List合并,过滤重复记录

    import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.I ...

  6. AsyncTask 与 Thread+Handler

    AsyncTask是封装好的线程池,比起Thread+Handler的方式,AsyncTask在操作UI线程上更方便,因为onPreExecute().onPostExecute()及更新UI方法on ...

  7. Quartz 并发/单线程

    Quartz 并发/单线程 Quartz定时任务默认都是并发执行的,不会等待上一次任务执行完毕,只要间隔时间到就会执行, 如果定时任执行太长,会长时间占用资源,导致其它任务堵塞.1.在Spring中这 ...

  8. jQuery 、js 设置 显示隐藏

    小问题   jQuery 操作方式: $("#ddl").parent().attr("style", "display:none"); j ...

  9. Java API —— 编码 & IO流( InputStreamReader & OutputStreamWriter & FileReader & FileWriter & BufferedReader & BufferedWriter )

    1.编码     1)编码表概述         由字符及其对应的数值组成的一张表     2)常见编码表         · ASCII/Unicode 字符集:ASCII是美国标准信息交换码,用一 ...

  10. NFC(2)NFC、蓝牙和红外之间的差异

    NFC(2)NFC.蓝牙和红外之间的差异表