记一次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才会对不上,也不知道这么理解对不对。

相关推荐
alexhilton8 小时前
面向开发者的系统设计:像建筑师一样思考
android·kotlin·android jetpack
CYRUS_STUDIO17 小时前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
CYRUS_STUDIO17 小时前
Frida 实战:Android JNI 数组 (jobjectArray) 操作全流程解析
android·逆向
用户0920 小时前
Gradle Cache Entries 深度探索
android·java·kotlin
循环不息优化不止20 小时前
安卓 View 绘制机制深度解析
android
叽哥20 小时前
Kotlin学习第 9 课:Kotlin 实战应用:从案例到项目
android·java·kotlin
雨白1 天前
Java 线程通信基础:interrupt、wait 和 notifyAll 详解
android·java
诺诺Okami1 天前
Android Framework-Launcher-UI和组件
android
潘潘潘2 天前
Android线程间通信机制Handler介绍
android
潘潘潘2 天前
Android动态链接库So的加载
android