简体   繁体   English

自定义工具栏中的“溢出菜单”图标

[英]Overflow Menu icon in Custom Toolbar

I am using a custom toolbar which has a menu icon. 我正在使用具有菜单图标的自定义工具栏。 Now on clicking this menu icon i want to show the options menu. 现在单击此菜单图标,我要显示选项菜单。 How can this be done. 如何才能做到这一点。

I tried adding a onclicklistener to this menu icon 我尝试将onclicklistener添加到此菜单图标

@Override
public void onClick(View v) {


    if(v.getId() == R.id.toolbarMenuIcon){
        openOptionsMenu();
    }

}

This didnt work. 这没有用。 Then i added these lines 然后我添加了这些行

setSupportActionBar(mBinding.customSelectToolbar.selectionModeToolbar);

in my activitys oncreate() . 在我的活动中oncreate()。 Also did override 也没有覆盖

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.selection_mode_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

With this i can see the overflow menu when i click the icon. 这样,当我单击图标时,我可以看到溢出菜单。 But the problem is that, it adds the default menu icon also to the tool bar and thus my tool bar has two menu now. 但是问题在于,它还在工具栏上添加了默认菜单图标,因此我的工具栏现在有两个菜单。 How can i have only my custom toolbar icon open the options menu 我怎样才能只有我的自定义工具栏图标打开选项菜单

If you want to customize the default overflow menu icon.. 如果要自定义默认的溢出菜单图标。

Use setOverflowIcon method of your toolbar. 使用工具栏的setOverflowIcon方法。

like: 喜欢:

 toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon));

I got it working using theme 我使用主题工作

<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_menu_overflow</item>
    <item name="android:tint">...</item>
    <item name="android:width">..dp</item>
    <item name="android:height">..dp</item>

</style>

<style name="OnArrival.toolbarTheme" parent="Theme.OnArrival.Light">

    <item name="actionOverflowButtonStyle">@style/OverFlow</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM