android L 新控件侧滑菜单DrawerLayout 使用教程
介绍
drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产物。drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),主内容区的内容可以随着菜单的点击而变化(这需要使用者自己实现)。
使用步骤:
创建一个DrawerLayout
为了添加导航抽屉,你需要在你的布局界面中声明一个DrawerLayout对象作为布局的根节点。同时在DrawerLayout内部添加两个view:
- 添加一个View,它包含应用的主内容(当抽屉隐藏时你的主要布局);
- 添加另一个View它包含了导航抽屉;
如下面例子所示:该布局使用了DrawerLayout它包含了两个子节点:一个
FrameLayout它包含了主要内容(在运行时将会被Fragment替换) 和 一个ListView作为导航抽屉,上面titlebar 上图标,负责打开、关闭抽屉;<?xml version="1.0" encoding="utf-8"?>
<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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="@+id/titleBar"
android:gravity="center_vertical"
android:background="@android:color/darker_gray"
android:layout_height="40dp"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_drawer"
android:onClick="onClickDrawerOpened"
android:clickable="true"
android:id="@+id/imageView" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_below="@id/titleBar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view --> <FrameLayout
android:id="@+id/content_frame" android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout> <!-- The navigation drawer -->
<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></span>上面这个例子包含了一些重要的布局技巧:
- 主内容View(FrameLayout在最上层)必须是
Drawerlayout的第一个子节点因为XML在安排这些界面的时候是按照Z轴的顺序来安排的 同时 抽屉必须在主内容的顶部。 - 主内容View被设置成匹配父View的宽和高,因为当导航抽屉隐藏的时候它要填充整个UI。
- 导航View(ListView)必须被声明一个水平的gravity借助属性
android:layout_gravity。为了满足从右到左的约定,声明它的值为”start” 代替 “left”(因此这个抽屉将会在右面呈现当布局是RTL时) - 在导航View声明时:宽度用dp为单位、高度匹配父View。为了保证用户无论怎样都能看到主内容的一部分,导航抽屉的宽度不能超过320dp
初始化Drawer List
在你的Activity中,要做的第一件事是初始化导航抽屉的列表项。具体该怎么做根据你APP的内容来定,但是导航抽屉通常包含一个Listview,所以还需要一个相匹配的Adapter(比如 ArrayAdapter 或者 SimpleCursorAdapter)
下面的例子,告诉你该如何借助一个string array 来初始化一个导航list
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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());
}
//................................
}
处理导航点击事件
当用户选择了抽屉列表里面的一个Item时, 系统调用onItemClickListener上的onItemClick(), 给setOnItemClickListener().
你在onItemClick()方法里面做什么, 取决于你的app实现的结构. 在下面的例子中, 选择每一个Item都会在主要内容的布局中插入一个不同的Fragment.
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) {
// update the main content by replacing fragments
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);
mDrawerLayout.closeDrawer(mDrawerList);
}
打开和关闭抽屉
使用onDrawerOpened()和onDrawerClosed () 打开和关闭抽屉:
public void onClickDrawerOpened(View drawerView) {
if(!mDrawerLayout.isDrawerOpen(GravityCompat.START))//if not open ,open or close;
{
mDrawerLayout.openDrawer(mDrawerList);
}
else
{
mDrawerLayout.closeDrawer(mDrawerList);
}
}
效果图:
android L 新控件侧滑菜单DrawerLayout 使用教程的更多相关文章
- Android L新控件RecyclerView简介
Android L是android进化史上的里程碑,尽管还没有正式发布4.5或者5.0,但预览版也同样精彩. 这篇文章只是另外一篇博客的总结性翻译,能够读懂原文的,可以点开这个链接去阅读精彩的原文:h ...
- android L新控件RecyclerView详解与DeMo[转]
http://blog.csdn.net/codebob/article/details/37813801 在谷歌的官网我们可以看到它是这样介绍的: RecyclerView is a more a ...
- android L新控件RecyclerView具体解释DeMo
简介 在谷歌的官方网站上,我们可以看到,它是此演示文稿:RecyclerView is a more advanced and flexible version of ListView. This w ...
- 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...
- Android其它新控件 (转)
原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...
- 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...
- android design 新控件
转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...
- [转 载] android 谷歌 新控件(约束控件 )ConstraintLayout 扁平化布局
序 在Google IO大会中不仅仅带来了Android Studio 2.2预览版,同时带给我们一个依赖约束的库. 简单来说,她是相对布局的升级版本,但是区别与相对布局更加强调约束.何为约束,即控件 ...
- Android Design Support控件之DrawerLayout简单使用
DrawerLayout能够让我们在项目中非常方便地实现側滑菜单效果.如今主流的应用如QQ等都 採用的这样的效果. 这两天也是在学习Android Design Support的相关知识.网上有关这方 ...
随机推荐
- HTML 编辑器
使用 Notepad 或 TextEdit 来编写 HTML 可以使用专业的 HTML 编辑器来编辑 HTML: Adobe Dreamweaver Microsoft Expression Web ...
- 【jmeter】浅说 think time
接口每天被5000个人调用,同时在线500人,每天要被调用50000次. 过了没多久测试完成写了一份报告发给项目经理: 并发 | 响应时间 | 应用服务器cpu |数据库服务器cpu |TPS | ...
- nova分析(9)—— nova-novncproxy
nova提供了novncproxy代理支持用户通过vnc来访问虚拟机,用户可以通过websocket.java客户端或者spicehtml5来访问.通过websket访问虚拟机的功能已经集成到hori ...
- 【并发编程】AQS学习
一个简单的示例: package net.jcip.examples; import java.util.concurrent.locks.*; import net.jcip.annotations ...
- Ajax方法执行跳转或者加载操作系统报出这样错误Sys.WebForms.PageRequestManagerParserErrorException:如何让解决
当你在代码中使用Response.Redirect(); 或者Response.Write();难免会遇到Sys.WebForms.PageRequestManagerParserErrorExce ...
- TortoiseSVN 同步分支
对比工具可以使用 winmerge 和 beyond compare ,winmerge免费小巧,beyond compare功能更强大.这两款工具都比TortoiseSVN自带的对比工具要好一些. ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Java事务处理全解析(一)——Java事务处理的基本问题
Java中的事务处理有多简单?在使用EJB时,事务在我们几乎察觉不到的情况下发挥着作用:而在使用Spring时,也只需要配置一个TransactionManager,然后在需要事务的方法上加上Tran ...
- JAVA使用HBASE常用方法
package HBaseTest; /** * Created by root on 11/11/22. */ import java.io.IOException; import org.apac ...
- PLSQL_PLSQL调优健康检查脚本SQLHC(案例)
2014-08-23 Created By BaoXinjian