2016-10-08 70 views
0

喜添加分享按鈕符號我已經做了我的應用程序 下面我能夠分享我的內容,但如果我對股票點擊它來自menuinflator不是符號想在我的應用程序

Image shows that menuinflator has share button

按鈕,我可以分享,但我想要共享符號,如何操作? 我已閱讀以下鏈接 https://developer.android.com/training/sharing/shareaction.html 但符號不來。

我的代碼如下

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 


    Menu xyz; 
    TextView t1 ; 
    private ShareActionProvider mShareActionProvider; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.t1 = (TextView)findViewById(R.id.textView); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate menu resource file. 
     getMenuInflater().inflate(R.menu.menu_filexml, menu); 

     menu.getItem(0).setVisible(true); 
     this.xyz = menu; 

     // Return true to display menu 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 

      case R.id.menu_item_share : 
       String code = " "; 
       code = this.t1.getText().toString(); 
       Intent sendIntent = new Intent(); 
       sendIntent.setAction("android.intent.action.SEND"); 
       sendIntent.putExtra("android.intent.extra.TEXT", code); 
       sendIntent.setType("text/plain"); 
       startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 

    } 
} 

菜單資源文件(menu_filexml.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:showAsAction="ifRoom" 
     android:title="Share" 
     android:actionProviderClass= 
      "android.widget.ShareActionProvider" /> 

</menu> 

此代碼,因此可在圖像,但公用符號給我的工作不來

+0

@Enzokie雅我的意思是在App –

+0

的頂部右側的共享圖標你需要在你的可繪製文件夾中添加一個圖標,然後像這樣做'android:icon =「@ drawable/my_menu_item_icon」' – Enzokie

+0

@Enzokie它不工作 –

回答

0

您需要使用自定義視圖爲您的菜單項。只是創建一個佈局文件並將其命名爲「my_item_custom_view」,並把你的圖標爲「ImageView的」在它然後分配給像下面的佈局文件:

<item 
android:id="@+id/menu_share" 
android:title="share" 
app:showAsAction="always" 
app:actionLayout="@layout/my_item_custom_view"/> 
相關問題