在res目錄下創(chuàng)建menu文件夾湖员,再在menu文件夾下新建一個(gè)menu.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/backup"
android:icon="@drawable/ic_backup"
app:showAsAction="always"
android:title="Backup"/>
<item
android:id="@+id/delete"
android:icon="@drawable/ic_delete"
app:showAsAction="ifRoom"
android:title="Delete"/>
<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings"
app:showAsAction="never"
android:title="Settings"/>
</menu>
其中android:showAsAction=""表示是否在標(biāo)題欄展示。always:總是展示,ifRoom:有空間時(shí)展示纫谅,never:從不偎漫。
在activity.java中重寫onCreateOptionsMenu()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toobar, menu);
return true;
}
添加點(diǎn)擊事件,重寫onOptionsItemSelected()
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.backup:
break;
}
return super.onOptionsItemSelected(item);
}