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,并且提供了 ...
随机推荐
- Putty查看生产环境下程序后台日志
双击桌面的putty.exe,然后选择session会话.登录用户.一般用户名为root. 登录成功之后在黑窗口敲以下命令行: cd /temp 到指定目录ls 查看该目录下文件tail -f n ...
- 脱产班第五次大作业-FTP服务器
下载项目 my_ftp #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import hmac import json import ...
- Tex_安装_在Ubuntu系统下
$\LaTeX$是一个强大的排版软件,在数学公式.表格.甚至是科学绘图方面有着独特优势.本文在Ubuntu系统下,整理Tex安装相关的操作,以为备忘.所引链接都未同作者商量,如有不妥望及时告知. 命令 ...
- 读取Excel表格中数据原型
写下这篇博客来记录自己的工作,这部分功能是读取Excel表格中的数据,并通过c#中的datagridview控件将读取的数据显示出来.为了方便用户,我设计了一个read按钮,用户点击这个按钮,会弹出打 ...
- React Native 安卓 程序运行报错: React Native version mismatch(转载)
这个问题已经得到解决,参照stackoverflow上的问题:https://stackoverflow.com/que...这个问题的原因就处在Android工程中app/build.gradle中 ...
- yii2 自带分页使用
//下面为控制器层的方法内容use \yii\db\Query;use \yii\data\Pagination; //方法内容 $query=new Query();//from为自带 ...
- freebsd配置ip 网关 子网掩码 DNS
1.设置IP地址.网关ee /etc/rc.conf #编辑ifconfig_em0="inet 192.168.1.173 netmask 255.255.255.0" ...
- github 的ssh key
一.输入cd ~/.ssh——回车(看你是否有了ssh key 密钥): 二.若无密匙,输入ssh-keygen -t rsa -C "your email"——直接回车,回车,跟 ...
- Java面向对象 第5节 抽象类和接口
一.抽象类和抽象方法 区分抽象方法和普通方法1)当一个方法被abstract修饰时,该方法成为抽象方法2)抽象类所在的类必须定义为抽象类3)抽象方法不会有具体的实现,而是在抽象类的子类中通过方法重写进 ...
- ueditor的简单用法
先粘贴未使用ueditor之前的代码: <body> <label for="input_content">作答区:</label> <t ...