我有問題。我按照很多指南,但仍無法讓我的「分享」按鈕正常工作。我在ActionBar中顯示了圖標,但是當我按下android:showAsAction =「always」時沒有任何事情發生。但是,當android:showAsAction =「從不」它有效。我只是想分享圖標始終顯示在ActionBar中。我做錯了什麼?請幫助在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>
按照這一辦法我必須知道的數據共享選項菜單時被建造。但是當我真正點擊按鈕時,只知道要共享哪些數據? – Martin