安卓中的底部共享菜单

5

在 Android 应用程序中共享数据时,我看到多个应用程序使用底部工作表(例如 Google I/O 2015)来指示应用程序执行操作,而不是包括处理您的意图的应用程序的标准对话框。

例如:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TITLE, "Some title..");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Some text");
startActivity(Intent.createChooser(shareIntent, "Share via.."));

由于操作系统版本选择器的显示,所以这种方法行不通。

有没有办法修改此代码以按照材料设计获取共享底部表格?https://www.google.com/design/spec/components/bottom-sheets.html#bottom-sheets-contententer image description here

有人知道第三方库可以在旧的API版本上实现这一点吗?

我发现了https://github.com/soarcn/BottomSheet

但这只允许我从底部表格创建菜单。 我想实际上,我可以查找所有可以执行我尝试执行的操作的应用程序,并在此库之上手动添加菜单项,但我希望有更简单的方法,因为这不是必要的功能。


1
“does not do the trick”在Android M上(或多或少)可以解决问题。选择器的格式由操作系统决定,您无法自行控制。您可以使用PackageManagerqueryIntentActivities()来创建自己的UI。或者,我记得I|O 2015应用程序有源代码可用,尽管他们可能尚未发布它。 - CommonsWare
@CommonsWare 正确,我注意到这在不同的设备、操作系统和制造商之间是不同的。 - kandroidj
4个回答

15

这是同样底部工具栏在运行Android 6.0的Nexus 5上的效果。

在旧版或修改后的Android系统上可能会有所不同(例如:三星设备等)


代码

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out this dope website! - https://mikemiller.design/"); // Simple text and URL to share
sendIntent.setType("text/plain");
this.startActivity(sendIntent);

结果

在这里输入图片描述


3
我们的BottomSheet实现有一个通用组件,您可以使用它来指定一个意图/筛选器等,该组件的行为与Lollipop系统版本非常相似。 https://github.com/flipboard/bottomsheet

3
我结合了上面两个答案的方法:在M+中使用内置的Bottom Sheet和在API级别低于M的情况下使用https://github.com/flipboard/bottomsheet。 我更喜欢Android提供的带分享意图的内置Bottom Sheet,因为它可以部分地滑入,然后用户可以将其完全滑入。第三方Bottom Sheet库提供了向后兼容性,无需自己开发,因此您可以快速添加它。但是,我更喜欢内置的Bottom Sheet。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  Intent sendIntent = new Intent();
  sendIntent.setAction(Intent.ACTION_SEND);
  sendIntent.putExtra(Intent.EXTRA_TEXT, "Content"); 
  sendIntent.setType("text/plain");
  this.startActivity(sendIntent);
}else {
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("text/*");
  intent.putExtra(Intent.EXTRA_TEXT, "Content");
  BottomSheet share = BottomSheet.createShareBottomSheet(MainActivity.this, intent, "Title");
  share.show();
}

0
如果有人正在寻找Kotlin实现。
val shareIntent = Intent(Intent.ACTION_SEND).apply {
    type = "text/plain"
    putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name))
    val shareMessage = "The message you want to sent with this."
    putExtra(Intent.EXTRA_TEXT, shareMessage)
}
startActivity(Intent.createChooser(shareIntent, "Select app to share with"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接