android 点击按钮弹出下拉菜单,添加分享按钮点击后如何显示下拉分享菜单?

添加分享按钮后,点击如何弹出下拉菜单显示可以分享的应用。

@Override

public boolean onCreateOptionsMenu(Menu menu) {

//return super.onCreateOptionsMenu(menu);

getMenuInflater().inflate(R.menu.menu, menu);

MenuItem item = menu.findItem(R.id.menu_item_share);

shareActionProvider = (android.support.v7.widget.ShareActionProvider) MenuItemCompat.getActionProvider(item);

return true;

}

private void setshareIntent(Intent shareIntent){

if (shareActionProvider!=null){

shareActionProvider.setShareIntent(shareIntent);

}

}

private Intent createIntent(){

Intent shareIntent = new Intent();

shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

shareIntent.setAction(Intent.ACTION_SEND);

shareIntent.setType("text/plain");

shareIntent.putExtra(Intent.EXTRA_TEXT,"hello");

return shareIntent;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

//return super.onOptionsItemSelected(item);

switch (item.getItemId()){

case R.id.refresh:

updateWeather();

break;

case R.id.setting:

Intent intent = new Intent(MyApplication.getContext(),SettingsActivity.class);

startActivity(intent);

break;

case R.id.menu_item_share:

setshareIntent(createIntent());

default:

break;

}

return super.onOptionsItemSelected(item);

}

MainActicty继承AppCompatActivity

menu.xml文件

xmlns:app="http://schemas.android.com/apk/res-auto">

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:orderInCategory="100"

android:title="刷新" />

android:orderInCategory="100"

android:title="设置"/>

android:orderInCategory="100"

android:title="位置"/>

android:id="@+id/menu_item_share"

app:showAsAction="ifRoom"

android:title="share"

android:icon="@drawable/chrome48"

app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值