这里菜单的简单使用:
在res目录下新建menu文件夹(res-new-directory)menu,在menu下新建mian文件(new-menu resource file)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add_item1"
android:icon="@mipmap/ic_launcher"
android:title="增加1"></item>
<item android:id="@+id/add_item2"
android:title="增加2"></item>
<item android:id="@+id/add_item3"
android:title="增加3"></item>
</menu>
重写onCreateOptionsMenu()方法和onCreateOptionsMenu()方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_item1:
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.add_item2:
Toast.makeText(this, "2222", Toast.LENGTH_SHORT).show();
break;
case R.id.add_item3:
Toast.makeText(this, "333333", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}