2017-02-28 56 views
1

我增加了共享操作使用下面的代码ShareActionProvider显示两个图标的系统图标和共享图标

<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" 
    tools:context="com.example.esir.jualeader.aspirant.MainActivity"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never" /> 
    <item 
     android:id="@+id/action_finish" 
     android:orderInCategory="200" 
     android:title="Exit" 
     app:showAsAction="never" /> 
    <item 
     android:id="@+id/share" 
     android:title="Share" 
     app:actionProviderClass="android.support.v7.widget.ShareActionProvider" 
     app:showAsAction="ifRoom" 
     /> 
</menu> 

private ShareActionProvider mShareActionProvider; 
private void setShareIntent(Intent shareIntent){ 

    if (mShareActionProvider != null) { 
     mShareActionProvider.setShareIntent(shareIntent); 
    } 
} 
private Intent createShareIntent(){ 
    Intent actionsend=new Intent(); 
    actionsend.setAction(Intent.ACTION_SEND); 
    actionsend.putExtra(Intent.EXTRA_TEXT,"Please Download Jua Leader App From : http://mstarsinnovations.com"); 
    actionsend.setType("text/plain"); 
    return Intent.createChooser(actionsend,"Share The Jua Leader Using"); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main,menu); 
    // Locate MenuItem with ShareActionProvider 
    MenuItem item = menu.findItem(R.id.share); 
    // Fetch and store ShareActionProvider 
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item); 
    setShareIntent(createShareIntent()); 
    // Return true to display menu 
    return true; 
} 

结果是一个共享图标看起来如图图片enter image description here

为什么会出现其他图标,它是唯一可点击的图标。如何删除它? 任何帮助将得到高度评价。

+0

你觉得'ShareActionProvider'看起来像什么?如果你只是想分享按钮,那么你不需要'ShareActionProvider'。 – ianhanniballake

+0

我需要什么? –

+0

你想让它看起来像什么? – ianhanniballake

回答

2

这正是ShareActionProvider的样子。如果您只想分享按钮,请停止使用ShareActionProvider。即,更新XML删除ShareActionProvider

<item 
    android:id="@+id/share" 
    android:title="Share" 
    android:icon="@drawable/share" 
    app:showAsAction="ifRoom" 
    /> 

(你需要添加自己的@drawable/share到您的应用程序,如一个从material design icons)。

然后重写你的onOptionsItemSelected()方法来启动你的份额时,该菜单项被点击:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getMenuId()) { 
    case R.id.share: 
     Intent shareIntent = createShareIntent(); 
     startActivity(shareIntent); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

你不需要做任何事情在onCreateOptionsMenu除了夸大你的菜单。

+0

酷...什么是ShareActionProvider的使用呢? –

+0

[docs](https://developer.android.com/reference/android/support/v7/widget/ShareActionProvider.html#rankings)说得很清楚:“股票行动提供商保留了每个股票目标的排名,基于用户选择每一个的频率,用户选择目标越频繁,排名越高; **最常用的目标出现在应用栏中作为默认目标**“ – ianhanniballake

+0

感谢澄清 –

0

它显示您最近与该应用程序共享内容(图标显示在右侧分享图标)。