Android 支持操作栏不显示溢出菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20713806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android support action bar not showing overflow menu
提问by omega
My android app supports 2.2 and higher. I'm using the appcompat support library for the action bar, so it should only show if there are things that don't fit. I want my action bar to support the overflow button (the three vertical squares) that reveals a menu with the other items when clicked.
我的 android 应用程序支持 2.2 及更高版本。我正在为操作栏使用 appcompat 支持库,因此它应该只显示不适合的内容。我希望我的操作栏支持溢出按钮(三个垂直方块),单击时会显示带有其他项目的菜单。
In my menu file, I have three items set up. However on the app I only see two of them, and the overflow button is not showing as well.
在我的菜单文件中,我设置了三个项目。但是在应用程序上我只看到其中两个,并且溢出按钮也没有显示。
activity_menu.xml
活动菜单.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:icon="@drawable/checkbox"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings2"
android:icon="@drawable/checkbox_checked"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings3"
android:icon="@drawable/ic_launcher"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
</menu>
java file
java文件
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class Activity_Menu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_menu, menu);
//return true;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
manifest
显现
<activity
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
android:name="sord.ids_connect.Activity_Menu"
android:screenOrientation="portrait"
android:label="@string/title_activity_menu" >
</activity>
回答by eOnOe
General reference
一般参考
1 - Override onCreateOption and inflate the menu
1 - 覆盖 onCreateOption 并扩大菜单
Note
笔记
Doesn't matter if you return true or call super
返回 true 或调用 super 都没有关系
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_menu, menu);
return super.onCreateOptionsMenu(menu);
}
2 - Add the namespace to the menu.xml file.
2 - 将命名空间添加到 menu.xml 文件中。
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto"
3 - set the showAsAction to alwaysor ifRoom
3 - 将 showAsAction 设置为always或ifRoom
**yourapp**:showAsAction="ifRoom"
4 - if you are using appcompat make sure that you Activity extends ActionBarActivity, you don't have to change any value of the ActionBar to be able to see you menuOption in the bar.
4 - 如果您使用 appcompat 确保您的 Activity 扩展ActionBarActivity,您不必更改 ActionBar 的任何值即可在栏中看到您的 menuOption 。
Finally you will have something like [remember to user a proper namespace for yourapp]
最后,您将有类似[记住用户的正确的命名空间yourapp]
main.xml
主文件
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_action_settings"
android:title="@string/action_settings"
yourapp:showAsAction="ifRoom" />
on your Activity
在您的活动中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
回答by Rakeeb Rajbhandari
If you are using the support library you'll need to declare the showAsAction from your own namespace; which will look like this app:showAsAction
. You can refer to eOnOe's answer for this.
如果您使用支持库,则需要从您自己的命名空间声明 showAsAction;看起来像这样app:showAsAction
。您可以参考eOnOe 对此的回答。
Using android:showAsAction
won't work with the support library because it uses it's own implementation of the action bar rather than the framework's implementation of it.
Usingandroid:showAsAction
不会与支持库一起使用,因为它使用它自己的操作栏实现而不是框架的它的实现。
You can also always group items into the icon for example:
您还可以始终将项目分组到图标中,例如:
<item ...
android:icon="@drawable/abs__ic_menu_moreoverflow_holo_dark">
<menu>
<item .../>
<item .../>
</menu>
</item>
回答by horkavlna
Android show overflow menu only if your device doesn't have menu button. Of course you can create hack a do it default for everyone device but it is't recommended solution. And for your every items add android:showAsAction="never"
and this say your device show overflow menu if you don't have menu button.
仅当您的设备没有菜单按钮时,Android 才会显示溢出菜单。当然,您可以为每个设备创建 hack a do it 默认值,但这不是推荐的解决方案。对于您添加的每个项目android:showAsAction="never"
,这表示如果您没有菜单按钮,您的设备会显示溢出菜单。
回答by CommonsWare
Im testing on my phone, and when I press the menu button, I see the menu appear on the bottom with only 1 option that says "Settings"
我在手机上进行测试,当我按下菜单按钮时,我看到菜单出现在底部,只有 1 个选项显示“设置”
That is the overflow.
那就是溢出。
also why is only 1 options appearing there when I have 3 items in the menu file?
当我在菜单文件中有 3 个项目时,为什么只有 1 个选项出现在那里?
Because the other two are in the action bar. You specified ifRoom
, and there were room for two, not three.
因为另外两个在操作栏中。您指定了ifRoom
,并且有两个空间,而不是三个空间。
回答by eshbeata
You need to menu the menu layout as below
您需要菜单菜单布局如下
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_container"
android:title="Menu"
android:showAsAction="never">
<item
android:id="@+id/action_settings"
android:icon="@drawable/checkbox"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings2"
android:icon="@drawable/checkbox_checked"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings3"
android:icon="@drawable/ic_launcher"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
</item>
</menu>
回答by miten joshi
Change Your menu.xml with the following code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_container"
android:title="Menu"
android:showAsAction="always">
<item
android:id="@+id/action_settings"
android:icon="@drawable/checkbox"
android:title="@string/action_settings"
android:showAsAction="always"/>
<item
android:id="@+id/action_settings2"
android:icon="@drawable/checkbox_checked"
android:title="@string/action_settings"
android:showAsAction="always" />
<item
android:id="@+id/action_settings3"
android:icon="@drawable/ic_launcher"
android:title="@string/action_settings"
android:showAsAction="always" />
</item>
</menu>
回答by Prashanth Dn
I had the same problem. I solved it by:
我有同样的问题。我通过以下方式解决了它:
1) commenting return true;
1)评论 return true;
2) uncommenting return super.onKeyUp(keyCode, event);
2)取消注释 return super.onKeyUp(keyCode, event);
in public boolean onKeyUp(int keyCode, KeyEvent event)
公开的;当众 boolean onKeyUp(int keyCode, KeyEvent event)