gpt4 book ai didi

java - 共享操作提供程序未显示

转载 作者:行者123 更新时间:2023-12-02 03:39:17 25 4
gpt4 key购买 nike

这是我的菜单资源文件

<item
android:id="@+id/action_share"
android:title="@string/action_share"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider">
</item>
</menu>

这是我的java代码

public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider =(ShareActionProvider)menuItem.getActionProvider();
// setIntent("COOL NIKS");
return super.onCreateOptionsMenu(menu);
}

这不显示共享操作提供者。我该如何修复它?

最佳答案

以下是完整的工作示例。它使用 AppCompat 库。

您需要添加http://schemas.android.com/apk/res-auto菜单项 XML 的架构。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="@+id/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"
android:title="Share" />
</menu>

现在您必须更新您的 MainActivity 才能使用 ShareActionProvider

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the menu item.
MenuItem menuItem = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// Set share Intent.
// Note: You can set the share Intent afterwords if you don't want to set it right now.
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}

// Create and return the Share Intent
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com");
return shareIntent;
}

// Sets new share Intent.
// Use this method to change or set Share Intent in your Activity Lifecycle.
private void changeShareIntent(Intent shareIntent) {
mShareActionProvider.setShareIntent(shareIntent);
}
}

关于java - 共享操作提供程序未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37021244/

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