1.打开Project structure,选择app modules,切换到Dependencies添加com.android.support.design.26.0.0.alpha1

2.在layout中添加toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.homelink.testtoolbar4.MainActivity"> <android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--内容--> </android.support.constraint.ConstraintLayout>

3.去掉默认的ActionBar。

在values下styles.xml中添加样式:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- activity theme. 无ActionBar样式:-->
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
弹出菜单的样式:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

打开manifests下的AndroidManifest.xml加入android:theme="@style/AppTheme.NoActionBar"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.homelink.testtoolbar4"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
4.在Activity中添加Toolbar的设置:
public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// 设置图标
toolbar.setLogo(R.mipmap.ic_launcher);
// 设置标题
toolbar.setTitle("Title");
// 设置副标题
toolbar.setSubtitle("Sub title");
// 设置返回按钮图标
toolbar.setNavigationIcon(R.drawable.hwpush_ic_toolbar_back);
setSupportActionBar(toolbar);
// 添加默认返回按钮
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 返回按钮的点击事件
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "点击了左侧导航按钮", Toast.LENGTH_SHORT).show();
}
});
}
然后默认的ActionBar没有了
5.加入右边菜单项
添加menu,menu_default.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_edit"
android:title="create"
android:orderInCategory="1"
android:icon="@android:drawable/ic_menu_edit"
app:showAsAction="collapseActionView" />
<item
android:id="@+id/action_more"
android:title="more"
android:orderInCategory="2"
android:icon="@android:drawable/ic_menu_more"
app:showAsAction="ifRoom" />
</menu>
5.在MainActivity.java中重写两个方法,实现菜单及其按钮的点击事件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_default, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_edit:
Toast.makeText(getBaseContext(), "点击了edit图标", Toast.LENGTH_LONG).show();
break;
case R.id.action_more:
Toast.makeText(getBaseContext(), "点击了more图标", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

												

android toolbar使用记录的更多相关文章

  1. Android Toolbar中的title居中问题

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/167 Android Toolbar中的title居中问题 ...

  2. arcgis android 图上记录gps轨迹

    原文  arcgis android 图上记录gps轨迹 public class MainActivity extends Activity { MapView mMapView; Location ...

  3. [置顶] xamarin android toolbar(踩坑完全入门详解)

    网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...

  4. 【Android实战】记录自学自己定义GifView过程,能同一时候支持gif和其它图片!【有用篇】

    之前写了一篇博客.<[Android实战]记录自学自己定义GifView过程,具体解释属性那些事! [学习篇]> 关于自己定义GifView的,具体解说了学习过程及遇到的一些类的解释,然后 ...

  5. Android 环境搭建记录

    Android 环境搭建记录 官网 https://developer.android.com/ studio 下载地址 官方下载 jikexueyuanwiki 国内镜像 studio历史版本 安装 ...

  6. Android Toolbar 开发总结

    初识 Toolbar Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件 ,Google 非常推荐大家使用 Toolbar 来作为Android ...

  7. 开启我的Android之旅-----记录Android环境搭建遇到的问题

    在现在这个离不开手机的时代,对于手机APP的开发也是一个很大的市场,所以自己也想去探一探手机APP开发,在我们进行Android开发的第一步就是搭建环境,具体怎么搭建我就不说,这里记录一下在搭建环境的 ...

  8. 【Android】学习记录<1> -- 初识ffmpeg

    工作需要用到ffmpeg来进行Android的软编码,对这玩意儿一点都不了解,做个学习记录先. FFmpeg:http://www.ffmpeg.org Fmpeg is the leading mu ...

  9. Android ToolBar

    众所周知,在使用ActionBar的时候,一堆的问题:这个文字能不能定制,位置能不能改变,图标的间距怎么控制神马的,由此暴露出了ActionBar设计的不灵活.为此官方提供了ToolBar,并且提供了 ...

随机推荐

  1. .net ORM框架(Dapper简单应用)

    1.引入 Dapper.dll类库 2.创建书籍模型book using System; using System.Collections.Generic; using System.Linq; us ...

  2. Delphi 10.3.1 Secure File Sharing解决应用间文件共享

    Delphi 10.3.1 为Android项目提供了Secure File Sharing选择项,默认是False.这一项是设置什么呢? 原来,Android 7及以后的版本,为了加强OS的安全性, ...

  3. 近期面试总结(Android)

    关于近期面试总结(2018年下半年) 有些是老生常谈有些是没有遇到的. 1.HTTP和HTTPS的区别 HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全,为 ...

  4. git的优缺点

      git可以说是世界上最先进的版本控制系统,大多语句的执行为linux语句,也不难怪,,起初他就是为了帮助开发linux开发内核而使用. 我们先来说git的主要功能,知道了这个,我们也就知道了为什么 ...

  5. Linux命令行下:把程序放后台执行,以及从后台继续执行程序

    把任务放到后台用 & 和 Ctrl+z 让后台任务从停止状态转为运行状态用 bg %N 把后台任务调回到前台用 fg %N 查看所有任务用jobs

  6. 《Linux内核原理与分析》第六周作业

    课本:第五章 系统调用的三层机制(下) 中断向量0x80和system_call中断服务程序入口的关系 0x80对应着system_call中断服务程序入口,在start_kernel函数中调用了tr ...

  7. 机器学习算法中如何选取超参数:学习速率、正则项系数、minibatch size

    机器学习算法中如何选取超参数:学习速率.正则项系数.minibatch size 本文是<Neural networks and deep learning>概览 中第三章的一部分,讲机器 ...

  8. github二次认证接收短信的问题

    这两天登陆github都被提示说我账号的密码已经被归类,提醒我修改密码. 改密码的时候发现GitHub有个二次认证的功能,分别是app和sms短信认证.app这里就不讲了,我们讲一下github的短信 ...

  9. 【HDU5187】contest

    真的没有什么会写的东西了QAQ 原题: As one of the most powerful brushes, zhx is required to give his juniors n probl ...

  10. asp.net 动态更改 Request.Header

    public class Dev_Sim: IHttpModule { public void Init(HttpApplication app) { app.BeginRequest += dele ...