Android 安卓共享按钮
在我根据您的回答更新了我的问题后,这是我的课堂:Android 安卓共享按钮,android,share-button,Android,Share Button,在我根据您的回答更新了我的问题后,这是我的课堂: public class ShareScreen extends Activity{ private ShareScreenAdapter adapter; private ViewPager viewPager; private String[] mThumbIds = Constants.IMAGES; @Override protected void onCrea
public class ShareScreen extends Activity{
private ShareScreenAdapter adapter;
private ViewPager viewPager;
private String[] mThumbIds = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_screen);
viewPager = (ViewPager) findViewById(R.id.pager);
ImageButton sharingButton = new ImageButton(this);
sharingButton.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sharingButton.setImageResource(R.drawable.share);
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new ShareScreenAdapter(ShareScreen.this, mThumbIds);
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareIt();
break;
default:
break;
}
return true;
}
private void shareIt()
{
//sharing implementation here
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
在menu/main_share.xml中,我有共享按钮:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_share"
android:title="Share"
android:showAsAction="always"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
还是不行!问题是我在actionbar上的shareButton不起作用。我点击它,但什么也没发生 没有堆栈跟踪很难调试,但是从您目前为止的代码来看,您还没有将sharingButton添加到视图中
[view you want to add the button to].addView(sharingButton)
我想你想做的是 首先,在CreateOptions菜单中创建
和选项项Selected
,这适用于在操作栏中单击的按钮。然后,填写代码以匹配您的目标,如下所示:
MainActivity.java
Menu.xml
您必须将此代码与布局中的视图相关联。否则,您将创建一个共享内容的按钮,但编译器将不知道它在您的布局中是哪个按钮。您可以发布堆栈跟踪吗?发布崩溃时的日志。您想在布局或操作栏中显示按钮吗?@Harry,在action barit works中!谢谢你,我对android开发还很陌生!再次感谢你
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_id, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
shareit();
break;
default:
break;
}
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/shareButton"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>