当前位置:网站首页>toolbar的使用
toolbar的使用
2022-07-25 07:32:00 【菜鸟xiaowang】
1.创建menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/addFriend"
android:icon="@drawable/addfriend"
android:orderInCategory="80"
android:title="添加朋友"
app:showAsAction="never" />
<item
android:id="@+id/newChat"
android:icon="@drawable/morechat"
android:orderInCategory="90"
android:title="新建群聊"
app:showAsAction="never" />
<item
android:id="@+id/scan"
android:icon="@drawable/scan"
android:orderInCategory="100"
android:title="扫一扫"
app:showAsAction="never"/>
<item
android:id="@+id/money"
android:icon="@drawable/money"
android:orderInCategory="100"
android:title="收付款"
app:showAsAction="never"/>
</menu>2.activity
布局引入toolbar
<androidx.appcompat.widget.Toolbar
android:background="@color/blue"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/toolbar"/>setSupportActionBar(mMainBinding.toolbar); 不调用就不会显示toolbar
getSupportActionBar().setTitle("环信 app"); 设置toolbar的标题
mMainBinding.toolbar.setOnMenuItemClickListener(listener); 设置监听器 重写onCreateOptionsMenu 加载menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
} 重写onPrepareOptionsPanel 加载menu的图标
@Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if (menu != null) {
if (menu.getClass() == MenuBuilder.class) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}监听器
private final Toolbar.OnMenuItemClickListener listener = new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.addFriend:
Toast.makeText(MainActivity.this, "addFriend", Toast.LENGTH_SHORT).show();
break;
case R.id.scan:
Toast.makeText(MainActivity.this, "scan", Toast.LENGTH_SHORT).show();
break;
case R.id.newChat:
Toast.makeText(MainActivity.this, "newChat", Toast.LENGTH_SHORT).show();
//弹出自定义popWindow
//popUpMyOverflow();
break;
case R.id.money:
Toast.makeText(MainActivity.this, "money", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
};边栏推荐
- UXDB怎么从日期值中提取时分秒?
- RPC通信原理与项目技术选型
- PADS导出gerber文件
- 数据提交类型 Request Payload 与 Form Data 的区别总结
- Common cross domain scenarios
- 使用CycleGAN训练自己制作的数据集,通俗教程,快速上手
- Wechat applet wx.request interface
- 【Unity入门计划】基本概念-触发器 Trigger
- A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
- Bingbing's learning notes: classes and objects (Part 1)
猜你喜欢

J1 common DOS commands (P25)

第一启富金怎么样

Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)

Completely replace the redis+ database architecture, and JD 618 is stable!

Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking

QT learning diary 20 - aircraft war project

From the era of portal to the era of information flow, good content has been ignored?

How to use network installation to deploy multiple virtual servers in KVM environment
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (V)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (V)

冰冰学习笔记:类与对象(上)
随机推荐
【程序员2公务员】一、基本认知
【程序员2公务员】关于体制调研的一些常见问题总结
Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking
集群聊天服务器:项目问题汇总
【程序员2公务员】三、资源搜集
js无法获取headers中Content-Disposition
Wechat applet request requests to carry cookies to verify whether it has logged in
Luo min from qudian, prefabricate "leeks"?
Matlab self programming series (1) -- angular distribution function
Learn no when playing 9 | enterprise knowledge management is so simple because it uses
MATLAB自编程系列(1)---角分布函数
Load capacity - sorting out the mind map that affects load capacity
Wechat applet wx.request interface
Security compliance, non-stop discounts! High quality travel service, "enjoy the road" for you
冰冰学习笔记:类与对象(上)
leetcode刷题:动态规划06(整数拆分)
【刷题笔记】搜索旋转排序数组
Analysis of common classes of Servlet
[notes for question brushing] search the insertion position (flexible use of dichotomy)
《游戏机图鉴》:一份献给游戏玩家的回忆录