android toolbar使用记录
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使用记录的更多相关文章
- Android Toolbar中的title居中问题
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/167 Android Toolbar中的title居中问题 ...
- arcgis android 图上记录gps轨迹
原文 arcgis android 图上记录gps轨迹 public class MainActivity extends Activity { MapView mMapView; Location ...
- [置顶]
xamarin android toolbar(踩坑完全入门详解)
网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...
- 【Android实战】记录自学自己定义GifView过程,能同一时候支持gif和其它图片!【有用篇】
之前写了一篇博客.<[Android实战]记录自学自己定义GifView过程,具体解释属性那些事! [学习篇]> 关于自己定义GifView的,具体解说了学习过程及遇到的一些类的解释,然后 ...
- Android 环境搭建记录
Android 环境搭建记录 官网 https://developer.android.com/ studio 下载地址 官方下载 jikexueyuanwiki 国内镜像 studio历史版本 安装 ...
- Android Toolbar 开发总结
初识 Toolbar Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件 ,Google 非常推荐大家使用 Toolbar 来作为Android ...
- 开启我的Android之旅-----记录Android环境搭建遇到的问题
在现在这个离不开手机的时代,对于手机APP的开发也是一个很大的市场,所以自己也想去探一探手机APP开发,在我们进行Android开发的第一步就是搭建环境,具体怎么搭建我就不说,这里记录一下在搭建环境的 ...
- 【Android】学习记录<1> -- 初识ffmpeg
工作需要用到ffmpeg来进行Android的软编码,对这玩意儿一点都不了解,做个学习记录先. FFmpeg:http://www.ffmpeg.org Fmpeg is the leading mu ...
- Android ToolBar
众所周知,在使用ActionBar的时候,一堆的问题:这个文字能不能定制,位置能不能改变,图标的间距怎么控制神马的,由此暴露出了ActionBar设计的不灵活.为此官方提供了ToolBar,并且提供了 ...
随机推荐
- Windows下dos命令行
Dos命令--基本命令 1.求助命令 例如:del /? rd /? 2.基本常用命令 例如:dir :列出当前目录下的文件以及文件夹,后面可以接其他路径 md:创建目录(mkdir),一次创建多级目 ...
- Problem D: 类的初体验(IV)
Description 定义一个类Data,只有一个int类型的属性和如下方法: 1. 缺省构造函数,将属性初始化为0,并输出"Data's default constructor.&q ...
- bfs两种记录路径方法
#include<cstdio> #include<queue> using namespace std; struct sss { int x,y; }ans[][]; ][ ...
- MFC VC++ 根据文件名获取程序的Pid
环境:PC Win7 VS VC++ .MFC 使用,输入文件名即可获取程序的pid,进而可以对程序进行操作,比如关闭Porcess等. 头文件: #include <TlHelp32.h> ...
- 【转】FMX 动态创建及销毁(释放free)对象
http://www.2pascal.com/thread-3037-1-1.html这是原文地址. (* ********************************************** ...
- 2019-04-10-day029-粘包处理
内容回顾 osi五层协议 不是真实存在的,只是抽象出来的模型 应用层 传输层 TCP/UDP TCP :全双工,可靠的,面向连接的,速度慢,对数据大小没有限制 建立连接 :三次握手 SYN ACK 断 ...
- Windows10 VS2017 C++ Json解析(使用jsoncpp库)
1.项目必须是win32 2.生成的lib_json.lib放到工程目录下 3.incldue的头文件放到工程目录,然后设置工程->属性->配置属性->vc++目录->包含目录 ...
- erlang-gb_tree,gb_set
gb_tree, gb_set, 均为一个二叉树.具体怎么实现,这边不在累赘,官方有手册, how to use ? 才是我们的重点 1. 初始化 1> gb_trees:empty().{0, ...
- Chromium Settings页面修改
/********************************************************************** * Chromium Settings页面修改 * 说明 ...
- 各机器学习方法代码(OpenCV2)
#include <iostream> #include <math.h> #include <string> #include "cv.h" ...