gpt4 book ai didi

android - ShareActionProvider 在 android 中没有任何操作栏

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:47 25 4
gpt4 key购买 nike

我不想在我的应用中使用操作栏,但仍希望使用操作栏提供的共享按钮。

这是在操作栏存在时完成的。

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
ShareActionProvider provider = (ShareActionProvider)
menu.findItem(R.id.menu_share).getActionProvider();

if (provider != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "hi");
shareIntent.setType("text/plain");
provider.setShareIntent(shareIntent);
}

return true;
}

menu.xml 保存在菜单文件夹中。

我想在我的 xml 中定义一个我自己的共享按钮,其中还定义了其他布局。

有什么帮助吗?

最佳答案

您不需要操作栏来共享内容。事实上,即使有 Action Bar,大多数应用程序也不使用 ShareActionProvider,因为视觉设计师讨厌它并且它不支持用户设备上的许多最新共享功能(例如直接共享联系人)。相反,您应该使用 Intent.createChooser 来创建更强大的共享对话框。

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

http://developer.android.com/training/sharing/send.html

在应用程序的任何位置共享的更好方法是使用 ShareCompat。这是一个简单的例子:

ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText("I'm sharing!")
.startChooser();

其他例子可以在这里找到:https://android.googlesource.com/platform/development/+/master/samples/Support4Demos/src/com/example/android/supportv4/app/SharingSupport.java

关于android - ShareActionProvider 在 android 中没有任何操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17791970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com