ShareActionProvider with one icon - looking as simple actionitem

不打扰是莪最后的温柔 提交于 2019-12-10 14:54:58

问题


I want to dislay ShareActionProvider on ActionBar, but with custom look&feel. Only one simple share icon without borders and without most used app icon on the right. But providing popup menu with most used applications. Is there a simple way to do it without implementing own ShareActionProvider?


回答1:


OK so regardless of ActionBarSherlock first test to see if your creating your intent correctly, ABS uses the same code as the generic chooser does so see if the app's you are looking for show up when you execute this code.

Intent I= new Intent(Intent.ACTION_SEND);
I.setType("text/plain");
I.putExtra(android.content.Intent.EXTRA_TEXT, "My Test Text");

startActivity(Intent.createChooser(I,"Share using ..."));

All of that app's that handle plain text will show up, if facebook, or whatever you are expecting is not there then those app's don't support the ACTION_SEND intent for the type you have registered (plain/text). (Facebook does, but more about that in a minute)

ABS has a sample for using the share action provider but it try's to send a photo, not a text message (status update) the setup you should be using is something like this

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate your menu.
    getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);

// Set file with share history to the provider and set the share intent.
MenuItem item = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider provider = (ShareActionProvider) item.getActionProvider();
              provider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
// Note that you can set/change the intent any time,
// say when the user has selected an image.
provider.setShareIntent(createShareIntent());

return true
}

And here is the intent that will be used to match app's and list them out from the sample

private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/plain");
        Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.putExtra(Intent.EXTRA_TITLE, "This is an android icon");
        return shareIntent;
    }

but you want it to be

private Intent createShareIntent() {
        Intent I= new Intent(Intent.ACTION_SEND);
        I.setType("text/plain");
        I.putExtra(android.content.Intent.EXTRA_SUBJECT, "TEST - Disregard");
        I.putExtra(android.content.Intent.EXTRA_TEXT, Uri.parse("http://noplace.com"));
    }

This should give you the same list in ABS at it did in the small test stub I showed with the chooser above.



来源:https://stackoverflow.com/questions/14745740/shareactionprovider-with-one-icon-looking-as-simple-actionitem

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