问题描述
我已经在Android Studio中制作了一个android应用程序,并想在其上创建一个选项菜单.我将其创建为空活动,现在意识到最好创建一个空白活动以获取选项菜单.无论如何,在空的活动中是否可以创建选项菜单.如果有人可以将我指向一个很棒的教程,那么这就是到目前为止我菜单中的代码.
I have made an android application in Android Studio and would like to create a option menu on it. I created it as an empty activity and now realize I would have been better creating a blank activity to get the option menu. Is there anyway to create the option menu in a empty activity. If someone could point me to a tutorial that would be great this is my code so far for my menu.
menu_menu
menu_menu
<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="saveourcar.soc.MainActivity">
<item
android:id="@+id/action_Menu"
android:orderInCategory="100"
android:title="Menu"
app:showAsAction="never" >
<menu>
<item
android:id="@+id/instructions"
android:title="Instructions"
android:icon="@drawable/bg"/>
<item
android:id="@+id/hotels"
android:title="Hotels"
android:icon="@drawable/mechanic"/>
</menu>
</item>
</menu>
主要活动
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_Menu) {
return true;
}
return super.onOptionsItemSelected(item);
}
推荐答案
您需要将菜单充气. 这些教程显示了如何使用菜单.所以像这样,并选择比menu_menu更好的名称:
You need to inflate your menu. These tutorials show how to use menus. So something like this, and choose a better name than menu_menu:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_menu, menu);
return true;
}
这篇关于将菜单添加到空活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!