记一次Toolbar和Menu配合使用的坑

记一次Toolbar和Menu配合使用的坑

在重构app页面中一些布局的时候,想着统一使用AppbarLayout和Toolbar作为标题栏来控制Activity的返回,但是其中有些Activity中是有使用Menu的,所以我试着在Toolbar中添加Menu,还真有一个属性可以设置Menu,想着这么方便就直接加上了

xml 复制代码
<com.google.android.material.appbar.AppBarLayout
        android:layout_height="60dp"
        android:layout_width="match_parent"
        android:theme="@style/Theme.Talk.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/moment_list_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:background="@color/teal_700"
            app:title="@string/moment"                             
            app:menu="@menu/moment"
            app:navigationIcon="@drawable/back"
            android:textAlignment="center"
            app:popupTheme="@style/Theme.Talk.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

上述代码中,我直接在Toolbar中添加了menu

如上图所示,是起了作用的

java 复制代码
//点击返回按钮
        activityMomentListBinding.momentListToolbar.setNavigationOnClickListener(v -> finish());

左侧返回Icon也是生效的,点击事件也是无问题,但是一点击Menu,问题就来了

java 复制代码
//选择图片新增朋友圈
        activityMomentListBinding.momentListToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.moment_menu_item){
                    imageChooseLauncher.launch(new PickVisualMediaRequest.Builder()
                            .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                            .build());
                }
                return true;
            }
        });
问题:

使用Toolbar自带的menu点击监听设置方法设置了监听,但是死活没去选择图片(代码中的逻辑),debug方法也是执行了,但是item.getItemId()与menu文件中的itemId不对。

What??? 怎么会呢?找资料找到怀疑人生,看到大家基本都是使用

java 复制代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    //在这里处理点击事件
    return super.onOptionsItemSelected(item);
}

这两个方法去处理menu的点击,我想着Toolbar不是都自带嘛,又可以设置menu,还可以设置监听,如果用回这个,我感觉用toolbar设置menu就有点多余了,所以我慢慢尝试,最后发现,我想达到的效果应该这样使用

解决方法:

在layout中设置toolbar,并设置menu

xml 复制代码
<com.google.android.material.appbar.AppBarLayout
        android:layout_height="60dp"
        android:layout_width="match_parent"
        android:theme="@style/Theme.Talk.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/moment_list_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:background="@color/teal_700"
            app:title="@string/moment"                             
            app:menu="@menu/moment"
            app:navigationIcon="@drawable/back"
            android:textAlignment="center"
            app:popupTheme="@style/Theme.Talk.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

在onCreate中添加

java 复制代码
setSupportActionBar(activityMomentListBinding.momentListToolbar);

然后重写onCreateOptionsMenu(Menu menu)方法

java 复制代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.moment_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

再设置Toolbar的menu点击监听就可以了

java 复制代码
activityMomentListBinding.momentListToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.moment_menu_item){
                    imageChooseLauncher.launch(new PickVisualMediaRequest.Builder()
                            .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                            .build());
                }
                return true;
            }
        });
原因:

我暂时没去深究,按照逻辑个人猜测应该是需要使用setSupportActionBar()设置Toolbar为ActionBar,然后再加载menu文件到上下文中,这样子Activity和Toolbar所使用的menu是同一个,点击事件下发处理时才会是相同的,如果就单纯在xml中设置menu,可能是加载menu文件然后新new了一个menu文件,所以menuItem的id才会对不上,也不知道这么理解对不对。

相关推荐
太空漫步112 小时前
android社畜模拟器
android
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
凯文的内存7 小时前
android 定制mtp连接外设的设备名称
android·media·mtp·mtpserver
天若子7 小时前
Android今日头条的屏幕适配方案
android
林的快手8 小时前
伪类选择器
android·前端·css·chrome·ajax·html·json
望佑8 小时前
Tmp detached view should be removed from RecyclerView before it can be recycled
android
xvch11 小时前
Kotlin 2.1.0 入门教程(二十四)泛型、泛型约束、绝对非空类型、下划线运算符
android·kotlin
人民的石头14 小时前
Android系统开发 给system/app传包报错
android
yujunlong391915 小时前
android,flutter 混合开发,通信,传参
android·flutter·混合开发·enginegroup
rkmhr_sef15 小时前
万字详解 MySQL MGR 高可用集群搭建
android·mysql·adb