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

相关推荐
咕噜企业签名分发-淼淼7 分钟前
如何实现安卓端与苹果端互通的多种方案
android
wu_android42 分钟前
Android 线性布局中常见的冲突属性总结
android
恋猫de小郭1 小时前
为什么跨平台框架可以适配鸿蒙,它们的技术原理是什么?
android·前端·flutter
张风捷特烈2 小时前
每日一题 Flutter#5,6 | 两道 Widget 选择题
android·flutter
移动开发者1号2 小时前
App主界面点击与跳转启动方式区别
android·kotlin
移动开发者1号2 小时前
我用Intent传大图片时竟然崩了,怎么回事啊
android·kotlin
androidwork14 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·java·kotlin·androidx
每次的天空14 小时前
Android第十三次面试总结基础
android·面试·职场和发展
wu_android14 小时前
Android 相对布局管理器(RelativeLayout)
android
李斯维16 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio