假设我的应用程序没有使用操作栏,当有当前设备没有硬件菜单按钮时,是否有人知道如何显示3点选项菜单?我已经看过下面的代码片段,但是它与Action Bar一起使用,对吗?
<!-- list_menu.xml -->
?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_item_1"
android:title="@string/menu_string_1"
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/menu_item_1"
android:title="@string/menu_string_2"
android:showAsAction="ifRoom|withText"/>
</menu>
在onCreateOptionsMenu
:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
谢谢!
答案 0 :(得分:3)
只是为了清理:你不想在你的应用程序中使用操作栏。如果设备有硬件菜单按钮,则您不想显示溢出图标(三个点)。如果设备没有硬件菜单按钮,你想要显示一个单独的溢出,对吗?
您可以使用
检查硬件菜单按钮ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();
您可以使用以下代码将侦听器放入此按钮:
@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
如果设备没有硬件菜单按钮,那么无论如何都可以插入此方法,这无关紧要。
这涉及手机有按钮的情况。如果它没有硬件按钮,则必须以编程方式在活动的onCreate()方法中添加一个。 所以你会在onCreate()中写下这样的东西:
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
if(!ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey()) {
Button overflowButton = new Button(this);
// Add further parameters to the button
// Especially the position (upper right) and the icon (overflow)
layout.addView(overflowButton );
}
溢出图标可以在这里下载: https://developer.android.com/design/downloads/index.html (它在动作栏图标包中)。
希望这会有所帮助:)
答案 1 :(得分:0)
该菜单显示在ActionBar
和&gt; = 3.0设备中。没有动作栏就行不通。对于较旧的&lt; 3.0设备,您可以使用ActioBarSherlock库来创建ActionBar
。
答案 2 :(得分:0)
您可以使用
检查设备中是否存在硬件菜单键ViewConfiguration.get(context).hasPermanentMenuKey()
并根据
显示您的图标