当style.xml中的主题为NoActionBar时如何添加菜单选项 [英] How to add menu options when theme is NoActionBar in style.xml
本文介绍了当style.xml中的主题为NoActionBar时如何添加菜单选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了 Fragments,所以我不得不使用 NoActionBar 主题,但是当我膨胀选项菜单时,它没有显示任何内容.如何解决这个问题?这是我的代码:
I have use Fragments so, I had to use theme NoActionBar, but when I inflate option menu it does not showing anything.How to resolve this? Here is my Code:
MainActivity.java
MainActivity.java
> @Override
> public boolean onCreateOptionsMenu(Menu menu){
> MenuInflater inflater =getMenuInflater();
> inflater.inflate(R.menu.options,menu);
> return super.onCreateOptionsMenu(menu);
> }
> @Override
> public boolean onOptionsItemSelected(MenuItem item){
> switch(item.getItemId()){
> case R.id.item1 :
> Intent box = new Intent(MainActivity.this,Developer.class);
> startActivity(box);
> break;
> }
> return true;
> }
样式.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
options.xml
options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Developer" android:id="@+id/item1"/>
</menu>
推荐答案
您可以使用 android.support.v7.widget.Toolbar#inflateMenu
方法向工具栏添加右侧菜单.这是 kotlin 的示例
You can add menu right to your toolbar using android.support.v7.widget.Toolbar#inflateMenu
method. This is example it kotlin
with(toolbar) {
inflateMenu(R.menu.menu_account_detail)
setOnMenuItemClickListener {
if (it.itemId == R.id.search) {
doSearch()
}
true
}
}
这篇关于当style.xml中的主题为NoActionBar时如何添加菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文