我制作了一个全新的项目,但是当我运行程序时,我的菜单没有出现。
这是我的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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".WeatherActivity">
<item
android:id="@+id/action_settings"
android:enabled="true"
android:orderInCategory="100"
android:title="@string/Град"
android:visible="true"
app:showAsAction="always" />
</menu>
这是我在WeatherActivity中的代码
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main5, menu);
return super.onCreateOptionsMenu(menu);
}
我无法制作菜单。我试过了:
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main5, menu);
return true;
/*getMenuInflater().inflate(R.menu.main5,menu);
return true;*/
/* MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main5, menu);
return super.onCreateOptionsMenu(menu);*/
}
答案 0 :(得分:0)
请参阅以下代码:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("LOG", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
//This line is important to show menu
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
答案 1 :(得分:0)
// in your activity
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
<强> menu.xml文件强>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="@+id/action_settings"
android:title="Setting" />
</menu>