toolbar2.png
1. 首先 在res目錄下新建menu文件夾,并創(chuàng)建menu文件:
share.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/toolbar_share"
android:icon="@drawable/ic_share_white_24dp"
android:title="share"
app:showAsAction="always" />
</menu>
app:showAsAction="always"急侥,這個(gè)屬性決定菜單是一直顯示還是在overflow中
2. 在Activity中初始化菜單,并處理選中事件:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//不同的界面可以根據(jù)需要填充不同的菜單
getMenuInflater().inflate(R.menu.share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.toolbar_share) {
Toast.makeText(this, "點(diǎn)擊了分享菜單", Toast.LENGTH_SHORT).show();
// TODO: 2017/11/9 實(shí)際的分享動(dòng)作
}
return true;
}