Hiding of the Toast for long press on actionBar it

2019-01-15 06:07发布

Is some possible way how to hide the toast after long-press on the ActionBar item? I didn't setup a title for the item but it is still there - empty toast.

<item
    android:id="@+id/ab_main_menu_dots"
    android:icon="@drawable/action_icons_dots"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/ab_main_menu_my_profile"
            android:showAsAction="never"
            android:title="@string/ab_my_profile">
        </item>
        <item
            android:id="@+id/ab_main_menu_settings"
            android:showAsAction="never"
            android:title="@string/menu_settings">
        </item>
        <item
            android:id="@+id/ab_main_menu_help"
            android:showAsAction="never"
            android:title="@string/tv_help_login">
        </item>
        <item
            android:id="@+id/ab_main_menu_about_us"
            android:showAsAction="never"
            android:title="@string/ab_about_us">
        </item>
        <item
            android:id="@+id/ab_main_menu_logout"
            android:showAsAction="never"
            android:title="@string/bt_logout_main">
        </item>
    </menu>
</item>

9条回答
女痞
2楼-- · 2019-01-15 06:44

Probably the cleanest way to go about this is to assign a custom action view to your menu item that mimics the look of a regular one.

Since you mentioned you're using ActionBarSherlock, here's a simple example.

Imagine the following menu.xml, which gets inflated in an Activity.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/ab_main_menu_dots"
        android:actionLayout="@layout/ab_main_menu_dots_layout"
        android:showAsAction="always"/>
</menu>

You can define ab_main_menu_dots_layout.xml to mimic the overflow button like this:

<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.Sherlock.ActionButton.Overflow"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

The result is a menu item that looks like an overflow button and does not display a Toast message when you long-press it, regardless of whether the native ActionBar is used or ABS. Up to you to take it from here. You want to reconsider and abide by the guidelines instead.

查看更多
地球回转人心会变
3楼-- · 2019-01-15 06:44

This is how I did it:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem item = menu.findItem(R.id.no_toast);
    item.setActionView(R.layout.custom_view);
    item.getActionView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //handle click (just this item)
        }
    });
    return true;
}

and this is my 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:title="Never gonna see me in a toast!"
        app:showAsAction="always"
        android:id="@+id/no_toast" />
</menu>

my custom view is just an ImageButton:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.AppCompat.Toolbar.Button.Navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/icon" />

Note: don't forget to set style, Widget.AppCompat.Toolbar.Button.Navigation makes IamgeButton to be shown correctly in toolbar.

P.S: Personally I prefer the default behavior but this was my case: I disabled right to left support for my application and after that when I set default locale to a rtl language, toast was showing up in the wrong side! Honestly I was in hurry and didn't find out the reason but I'll appreciate if someone let me know the why, anyway this is how I passed through.

查看更多
Deceive 欺骗
4楼-- · 2019-01-15 06:46

You could try to create your own custom ActionBar. Here is a tutorial on how to do that: Custom Action Bar

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-15 06:50

You can achieve this by using custom action view as follow:

action_menu.xml
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/item1"
        support:showAsAction="always">
    </item>

</menu>

custom_action_view.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:paddingRight="5dp" >

    <ImageButton
        android:id="@+id/customActionItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/abc_item_background_holo_dark"
        android:src="@drawable/bulb_icon" />

    </RelativeLayout>

and menu inflater code is as follow:

    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_menu, menu);     

        final MenuItem item1= menu.findItem(R.id.item1);
        MenuItemCompat.setActionView(item1, R.layout.custom_action_view);
        View vItem1= MenuItemCompat.getActionView(item1);

        final ImageButton customActionItem= (ImageButton) vItem1.findViewById(R.id.customActionItem);
        customActionItem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // do something here
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-15 06:52

For me the solution was using:

android.support.v4.view.MenuItemCompat

So instead of inflating the menu from the XML:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.refresh_menu, menu);
    return true;
}

I created the items programmatically using MenuItemCompat:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem refreshItem = menu.add(Menu.NONE, R.id.menu_item_refresh, Menu.NONE, R.string.general_pop_up_dialog_btn_cancel);
    MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_custom_view_refresh);
    MenuItemCompat.setShowAsAction(refreshItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
    return true;
}
查看更多
等我变得足够好
7楼-- · 2019-01-15 06:53

There is a way, if you're using ActionBarSherlock. Find the ActionMenuItemView.java file in library and just comment whole onLongClick method.

查看更多
登录 后发表回答