我有问题。我遵循了很多指南,但仍然无法让我的分享按钮工作。我在 ActionBar 中显示了图标,但是当我按下 android:showAsAction="always" 时什么也没有发生。但是当 android:showAsAction="never" 它工作。我只希望共享图标始终显示在 ActionBar 中。我做错了什么?请帮忙
这是我的代码:
public class Main extends Activity {
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.shareButton);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Check it out";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
return true;
case R.id.aboutButton:
Intent intent = new Intent(this, About.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
这是我的 menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="@string/share"
android:showAsAction="always"/> **---> when I put here "NEVER" then it works! But I want Share to be always as icon**
<item
android:id="@+id/aboutButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="never"
android:title="@string/about_dev"/>
</menu>