DrawerLayout和toolbar的使用
onPostCreate()是Activity完全启动后的调用:在完全启动后的回调设置toolbar 然后在使用
AppCompatActivity 时style要设置为何appCompat相关的样式,不然会报错:
You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
@Nullable doesn't work if Butterknife binds view from supersuperclass #389
android:layout_width="match_parent" android:layout_height="match_parent"不然也会报错:DrawerLayout must be measured with MeasureSpec.EXACTLY.解释: http://stackoverflow.com/questions/25864323/drawerlayout-must-be-measured-with-measurespec-exactly Toolbar 使用的style:
<style name="ToolbarThemem" parent="AppTheme.PopupOverlay">
    <item name="android:background">?attr/colorPrimary</item>  设置背景颜色    <item name="android:layout_width">match_parent</item>       <item name="android:layout_height">wrap_content</item>    <item name="android:fitsSystemWindows">true</item>         <item name="android:minHeight">?attr/actionBarSize</item>   设置最小高度
<!-- android:textColorPrimary is the color of the title text in the Toolbar, in the Theme.AppCompat theme: --><item name="android:textColorPrimary">@color/white</item> <!-- android:textColorPrimaryInverse is the color of the title text in the Toolbar, in the Theme.AppCompat.Light theme: --><item name="android:textColorPrimaryInverse">@color/white</item> <!-- android:actionMenuTextColor is the color of the text of action (menu) items in the Toolbar, at least in the Theme.AppCompat theme. For some reason, they already get the textColorPrimary when running on API 21, but not on older versions of Android, so this is only necessary to support older Android versions.--><item name="actionMenuTextColor">@color/white</item><!-- android:textColorSecondary is the color of the menu overflow icon (three vertical dots) --><item name="android:textColorSecondary">@color/white</item> <!-- This would set the toolbar's background color, but setting this also changes the popup menu's background, even if we define popupTheme for our <Toolbar> --> <item name="android:colorBackground">?attr/colorPrimary</item>
</style> AppTheme.PopupOverlay 是系统提供的一个默认Toolbar 的主题: <?xml version="1.0" encoding="utf-8"?>
<!--  android:minHeight="?attr/actionBarSize"/>系统的配置-->
<android.support.v4.widget.DrawerLayout    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:fitsSystemWindows="true"    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">    <android.support.v7.widget.Toolbar        android:id="@+id/msc_toolbar"      android:layout_height="wrap_content"        android:layout_width="match_parent"        android:layout_alignParentTop="true"        android:minHeight="?attr/actionBarSize"        app:popupTheme="@style/ThemeToolbar"        app:theme="@style/ThemeToolbar"        />
        <LinearLayout            android:orientation="vertical"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginTop="5dp"            android:layout_below="@id/msc_toolbar"            android:background="@color/white">
            <TextView                android:id="@+id/noThingsMessage"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_margin="30dp"                android:textSize="20dp"                android:gravity="center_horizontal"                android:visibility="gone"/>
<ListView android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/frame"/>
        </LinearLayout>    </RelativeLayout><android.support.design.widget.NavigationView    android:id="@+id/navigation_view"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:layout_gravity="start"    app:headerLayout="@layout/drawer_header"    android:background="?attr/colorPrimary"    app:itemIconTint="@color/white"    app:itemTextColor="@color/white"    app:menu="@menu/menu_main"    /></android.support.v4.widget.DrawerLayout>
//可以在onCreate() 或者onPostCreate() 方法中去显示 Toolbar 效果  :@Overrideprotected void onPostCreate(Bundle savedInstanceState) {    super.onPostCreate(savedInstanceState);
    mscToolbar = (Toolbar) findViewById(R.id.msc_toolbar);    navigationView = (NavigationView) findViewById(R.id.navigation_view);    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    //TODO eventually remove if everything has the bar    if (mscToolbar != null) {        setSupportActionBar(mscToolbar);        getSupportActionBar().setTitle(title);        getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.ic_menu_white_24dp));        getSupportActionBar().setDisplayHomeAsUpEnabled(true);        getSupportActionBar().setHomeButtonEnabled(true);
    }}
然后就是使用到了
navigationView.setNavigationItemSelectedListener(      new NavigationView.OnNavigationItemSelectedListener() {         @Override         public boolean onNavigationItemSelected(MenuItem menuItem) {            menuItem.setChecked(true);            drawerLayout.closeDrawers();            Intent intent = null;            switch (menuItem.getItemId()) {               case R.id.drawer_about:                  if (BaseActivity.this.getClass().equals(AboutActivity.class))                     return true;                  intent = new Intent(BaseActivity.this, AboutActivity.class);                  break;.......
//Toolbar 实现DrawerLayout 和图标互动的效果: 
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);drawer.setDrawerListener(toggle);toggle.syncState();
DrawerLayout和toolbar的使用的更多相关文章
- 修改DrawerLayout 和toolbar 配合navigation的颜色
		
大家都知道DrawerLayout 和toolbar 结合能出来高大上的效果. 使用到一个ActionBarDrawerToggle类. 那么怎么修改DrawerToggle的颜色呢,搜索了很多中文网 ...
 - DrawerLayout,ToolBar 和 TabHost 的使用
		
ActionBar 定义起来不方便 toolbar: 最重要的特性,显示menu菜单,右上角三个点的菜单按钮,使用灵活 使用:1,布局文件,包裹LinearLayout 放imageView, 或者I ...
 - Android——使用Toolbar + DrawerLayout快速实现高大上菜单侧滑(转)
		
今天就来使用官方支持库来快速实现这类效果,需要使用到Toolbar和DrawerLayout,详细步骤如下:(如果你还不知道这两个Widget,先自己Google吧~) 1.首先需要添加appcomp ...
 - 使用Toolbar + DrawerLayout快速实现高大上菜单侧滑
		
如果你有在关注一些遵循最新的Material Design设计规范的应用的话(如果没有,假设你有!),也许会发现有很多使用了看起来很舒服.很高大上的侧滑菜单动画效果,示例如下(via 参考2): 今天 ...
 - Android 使用Toolbar+DrawerLayout快速实现仿“知乎APP”侧滑导航效果
		
在以前,做策划导航的时候,最常用的组件便是SlidingMenu了,当初第一次用它的时候觉得那个惊艳啊,体验可以说是非常棒. 后来,Android自己推出了一个可以实现策划导航的组件DrawerLay ...
 - Toolbar的使用
		
项目来源: https://github.com/xuwj/ToolbarDemo#userconsent# 一.V7包升级问题 折腾好久,终于解决 <style name="AppT ...
 - 【Android - V】之DrawerLayout的使用
		
DrawerLayout是Android V4包中的一个布局控件,用来实现一个抽屉样式的布局. DrawerLayout通过设置子视图的layout_gravity来决定子视图停靠在屏幕的哪个边缘外侧 ...
 - Androd Toolbar 的简单使用(转)
		
14年Android开发者大会提出了Android5.0 系统以及 材料设置 Material Design.在 材料设计中推出了大量的UI效果,其中某些功能 已添加进 兼容包,所以可以在低版本中来实 ...
 - 【Android - 控件】之V - DrawerLayout的使用
		
DrawerLayout是Android V4包中的一个布局控件,用来实现一个抽屉样式的布局. DrawerLayout通过设置子视图的layout_gravity来决定子视图停靠在屏幕的哪个边缘外侧 ...
 
随机推荐
- php 相对路径中 及 绝对路径中 的一些问题
			
写本篇文章,是为了以后学习中遇到问题好解决 php的相对路径是以当前工作目录为基准的,并不是以当前处理的文件目录为基准,这样导致我们在开发过程中总会遇到一些问题. 但是如果我们使用绝对路径,就会导致后 ...
 - (转载)通过dbgrideh 从数据集中选择合适的记录
			
通过dbgrideh 从数据集中选择合适的记录 //---------------------------------------------------------// 通过dbgrideh 从数据 ...
 - POJ 1416 Shredding Company
			
题目: http://poj.org/problem?id=1416 又16ms 1A了,这人品... #include <stdio.h> #include <string.h&g ...
 - iOS开发之Runloop(转)
			
Objective-C之run loop详解 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9237973 转载请注明出 ...
 - iOS - Responder Chain
			
在iOS中,当发生事件响应时,必须知道由谁来响应事件.这就是由响应者链来对事件进行响应,所有事件响应的类都是UIResponder的子类,响应者链是一个由不同对象组成的层次结构,其中的每个对象将依次 ...
 - Duplicate Symbol链接错的原因总结和解决方法-b
			
duplicate symbol是一种常见的链接错误,不像编译错误那样可以直接定位到问题的所在.但是经过一段时间的总结,发现这种错误总是有一些规律可以找的.例如,我们有如下的最简单的两个类代码: // ...
 - JS简单仿QQ聊天工具的制作
			
刚接触JS,对其充满了好奇,利用刚学到的一点知识,写了一个简单的仿QQ聊天的东西,其中还有很多的不足之处,有待慢慢提高. 功能:1.在输入框中输入内容,点击发送,即可在上方显示所输入内容. 2.点击‘ ...
 - C/S和B/S两种软件体系结构
			
目前两种流行的软件体系结构就是C/S和B/S体系结构,下面对两种体系结构进行一下总结: 1.C/S(客户端/服务器模式): 客户端和服务器都是独立的计算机,客户端是面向最终用户的应用程序或一些接口设备 ...
 - Hbase案例分析(一)
			
Hbase应用场景: 1 随机读或者写 2 大数据上的高并发操作,比如每秒对PB级数据进行上千次操作.(查询,删除等操作) 3 读写均是非常简单的操作,比如没有join操作 Hbase Schema设 ...
 - windows远程桌面连接配置
			
我的电脑 -> 属性 -> 远程 把两个checkbox勾上 运行(win + r) -> 输入secpol.msc回车 -> 找到本地策略 -> 安全选项 ->账 ...