简介

Drawerlayout是google自带的控件,功能类似开源的SlidingMenu,在support-v4包下用来替代SlidingMenu(google好无耻啊)。

使用方法

在activity_main.xml中定义:

<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"> <!-- The main content view --> <FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout> <!-- The Navigation view -->
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffcc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
></ListView> </android.support.v4.widget.DrawerLayout>

注意事项:

  • drawerlayout的主视图必须要是DrawerLayout的第一个子视图。
  • 主视图宽度和高度匹配父视图,即match_parent,抽屉隐藏的时候,必须让主视图占满屏幕。
  • 必须指定抽屉的android:layout_gravity属性
  • 抽屉视图的宽度以dp为单位,最好不超过300dp

在drawerLayout中添加子项:

    private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ArrayList<String> menuLists;
private ArrayAdapter<String> mAdapter;
        for (int i = 0; i < 5; i++){
menuLists.add("Hello0" + i);
}
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuLists);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(this);

同时响应点击事件:

    @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//动态插入fragment到framelayout当中
Fragment contentFragment = new ContentFragment();
Bundle args = new Bundle();
args.putString("text", menuLists.get(i));
contentFragment.setArguments(args);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, contentFragment).commit();
mDrawerLayout.closeDrawer(mDrawerList);
}

根据点击位置设置动态添加的fragment中的文字:

public class ContentFragment extends Fragment {
private TextView mTextView; @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
mTextView = (TextView) view.findViewById(R.id.textView);
String text = getArguments().getString("text");
mTextView.setText(text);
return view;
}
}

为DrawerLayout打开关闭设置监听器:

  1. mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener);
  2. 2. ActionBarDrawerToggle是DrawerLayout.DrawerListener的具体实现。

    1.     设置android.R.id.home的图标
    2.     Drawer拉出,隐藏带有android.R.id.home动画效果
    3.   监听Drawer拉出还是隐藏事件

3. 覆盖ActionBarDrawerToggle的onDrawerOpened和onDrawerClosed()以监听抽屉拉出或隐藏事件。

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle("Please Choose");
} @Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActionBar().setTitle(mtitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

Android中的DrawerLayout的更多相关文章

  1. Android 中 DrawerLayout + ViewPager 怎么解决滑动冲突?

    DrawerLayout 是 Android 官方的侧滑菜单控件,而 ViewPager 相信大家都很熟悉了.今天这里就讲一下当在 DrawerLayout 中嵌套 ViewPager 时,要如何解决 ...

  2. Android抽屉效果 DrawerLayout 入门经验总结

    今天试了试这个抽屉布局的效果,结果很崩溃无语 网上很多资料都千篇一律,感觉都有问题,下面总结下几点经验: 先上个效果图: 1.  layout 布局文件中怎么写: <android.suppor ...

  3. Android组件——使用DrawerLayout仿网易新闻v4.4侧滑菜单

    摘要: 转载请注明出处:http://blog.csdn.net/allen315410/article/details/42914501 概述        今天这篇博客将记录一些关于DrawerL ...

  4. Android 使用Toolbar+DrawerLayout快速实现仿“知乎APP”侧滑导航效果

    在以前,做策划导航的时候,最常用的组件便是SlidingMenu了,当初第一次用它的时候觉得那个惊艳啊,体验可以说是非常棒. 后来,Android自己推出了一个可以实现策划导航的组件DrawerLay ...

  5. Android中的LinearLayout布局

    LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,  线性布局是按照垂直方向(vertical)或水平方向 ...

  6. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  7. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  8. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  9. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

随机推荐

  1. 前言,学习ios编程(坚持)

    其实,尝尝有人很疑惑,不知道自己要干嘛,看到很多的培训机构,不知道怎么选择但是又想进入软件行业.其实呢学习不一定要靠培训机构,一定要培训,特别是 当人家把自己吹的天花乱坠的时候,然并卵.出来之后,也只 ...

  2. Visual Studio(VS2012) Project&(Solution) 虚拟文件夹 & 物理文件夹

    今天发生个怪事:在 Solution Explorer 中,x project 内建立文件夹(folder)时,同时在磁盘目录下也创建了同名的文件夹. 1, 原本:应该只是创建一个“虚拟文件夹”用来“ ...

  3. 人工神经网络ANNs

    参考: 1. Stanford前向传播神经网络Wiki 2. Stanford后向传播Wiki 3. 神经网络CSDN blog 4. 感知器 5. 线性规划 6. Logistic回归模型 内容: ...

  4. (转载)Linux如何编译安装源码包软件

    一.什么是源码包软件: 顾名思义,源码包就是源代码的可见的软件包,基于Linux和BSD系统的软件最常见:在国内源可见的软件几乎绝迹:大多开源软件都是国外出品:在国内较为出名的开源软件有fcitx;l ...

  5. iOS - WXPay 微信支付

    1.微信支付申请 微信支付官方集成指引 微信支付官方集成指导视频 微信 APP 支付开发者文档 微信公众平台 微信开放平台 微信商户平台 1.1 微信 APP 支付申请步骤 APP 支付:APP 支付 ...

  6. Netscape HTTP Cooke File Parser In PHP

    http://www.hashbangcode.com/blog/netscape-http-cooke-file-parser-php I recently needed to create a f ...

  7. OpenCV2的Mat矩阵形式自定义初始化

    我们知道,OpenCV2的矩阵形式是Mat,那么Mat矩阵的初始化怎么自定义呢 ?由于比较简单,文字部分我就不多加说明了,见代码,有下面几种: //////////////////////////// ...

  8. htmlFormat

    import java.text.SimpleDateFormat; import org.apache.log4j.HTMLLayout; import org.apache.log4j.Layou ...

  9. Vim插件管理器Vundle使用

    参考地址:http://www.linuxidc.com/Linux/2012-12/75684.htm Vundle(Vim bundle) 是一个vim的插件管理器. 其Github地址为: ht ...

  10. centos 忘记密码

    装了个  centos 6.8  安装的时候 要输入 新用户和密码 用 新的用户密码 进去后 各种没权限  重新修改 root 密码   一切OK 步骤 1.重新启动Centos,在启动过程中,长按“ ...