I Use ShareActionProvider in PopupMenu, but show two PopupMenu?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 01:47:12

问题


I use a ShareActionProvider in a PopupMenu, but when I click the share menu item, it shows two PopupMenus on the screen, one covered by the other. And one shows the application icon and name, the other one only shows the application name.

It works fine except this problem...
How can I fix it?

P.S.: please forgive me for my bad expression

My code is:

PopupMenu popup = new PopupMenu(this, button);
popup.getMenuInflater().inflate(R.menu.scrawl_popup_menu, popup.getMenu());
MenuItem overflowItem = popup.getMenu().findItem(R.id.popup_share);
ShareActionProvider overflowProvider =
    (ShareActionProvider) overflowItem.getActionProvider();
overflowProvider.setShareHistoryFileName(
    ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
overflowProvider.setShareIntent(createShareIntent());

menu.xml is:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/popup_clear"
        android:icon="@drawable/ic_menu_clear"
        android:title="@string/popup_menu_clear" />
    <item android:id="@+id/popup_insert_bg"
        android:icon="@drawable/ic_menu_insert_bg"
        android:title="@string/popup_menu_insert_bg"/>
    <item android:id="@+id/popup_share"
        android:icon="@android:drawable/ic_menu_share"
        android:title="@string/popup_menu_share"
        android:actionProviderClass="android.widget.ShareActionProvider">
    </item>
</menu>

回答1:


I had to use startActivity(getShareIntent("/status.jpg")); This doesn't work exactly as you expect. However, it can be used for the same purpose. Hope it help.

private Intent getShareIntent(String filePath) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);

        File sdCard = Environment.getExternalStorageDirectory();

        File sharedFile = new File(sdCard + sharePath);
        Uri uri = Uri.fromFile(sharedFile);

        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        return shareIntent;
    }

However, finally I moved to use action bar with Selection patten instead: http://developer.android.com/design/patterns/selection.html



来源:https://stackoverflow.com/questions/11966542/i-use-shareactionprovider-in-popupmenu-but-show-two-popupmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!