This question already has an answer here:
这个问题在这里已有答案:
- Actionbar not shown with AppCompat 3 answers
- 操作栏未显示AppCompat 3的答案
I am using appcompat in my app. I want the menu items to show on actionbar or at least the overflow(3 dots) to show them when there is no room. There is lot of space on the actionbar, but still they don't show up. The menu flow raises from the bottom and that too only when menu button is pressed.
我在我的应用程序中使用appcompat。我希望菜单项显示在操作栏上或至少溢出(3个点)以在没有空间时显示它们。操作栏上有很多空间,但它们仍然没有显示出来。菜单流从底部开始,仅在按下菜单按钮时也是如此。
menu_activity.xml:
menu_activity.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_lang"
android:showAsAction="always"
android:title="@string/menu_lang"
android:icon="@android:drawable/ic_input_lang"/>
</menu>
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.menu_activity, menu);
return true;
}
This post says that it not works when hardware menu button is present. But other apps are able to show items on the same device. So, that answer seems to be incorrect. Can someone please help on this?
这篇文章说当硬件菜单按钮存在时它不起作用。但其他应用程序可以在同一设备上显示项目。所以,这个答案似乎是不正确的。有人可以帮忙吗?
3 个解决方案
#1
71
You seem to be using the wrong menu: Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu
您似乎使用了错误的菜单:您的文件名为“menu_activity.xml”,并使用Resource-Id扩展菜单:R.menu.reminder_menu
The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity
菜单的资源名称应与文件名相同,即:R.menu.manu_activity
Try it with this again - I ran into this too once and it drove me nuts...
再试一次 - 我也碰到了这一次它让我疯了......
Update After clarification that the above part was for obfuscation, please make sure that:
更新在澄清上述部分是为了混淆之后,请确保:
- You extend ActionBarActivity.
- 您扩展了ActionBarActivity。
- You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
- 您为活动(或整个应用程序)使用(或扩展)Theme.AppCompat主题之一
- Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the
showAsAction
attribute, so that the compatibility library can pick them up. - 因为在较旧的设备上,不存在与Actionbar相关的属性,所以请确保XML中的所有这些属性都与自定义命名空间一起使用。这将是showAsAction属性,以便兼容性库可以拾取它们。
You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction
tag to app:showAsAction
according to the Android guide.
您已经定义了自定义命名空间(菜单标记中的“app”)。你需要根据Android指南将android:showAsAction标签更改为app:showAsAction。
Your corrected menu_activity.xml would then look like this:
您更正后的menu_activity.xml将如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_lang"
app:showAsAction="always"
android:title="@string/menu_lang"
android:icon="@android:drawable/ic_input_lang"/>
</menu>
The extended Android guide for actionbar covers these new and nasty traps...
动作栏的扩展Android指南涵盖了这些新的和讨厌的陷阱......
#2
5
Okay I think I found a solution for you. It is called Overflow Menu and you need to call the below method in your onCreate method.
好吧,我想我找到了适合你的解决方案。它被称为溢出菜单,您需要在onCreate方法中调用以下方法。
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this and let me know if it works for you.
试试这个,让我知道它是否适合你。
EDIT:
编辑:
I think I understood your problem finally. I can give you one idea. If you do not want to use overflow menu and just display menu items as displayed in the screen-shot you have posted, you can create a layout on top of your activity.
我想我终于理解了你的问题。我可以给你一个想法。如果您不想使用溢出菜单并只显示已发布的屏幕截图中显示的菜单项,则可以在活动之上创建布局。
You can take a linear layout, give a background that looks the same as action bar and place whatever icons you want to put there, and give functionality to them with onClickListener. Hope this will give you some idea. This way you can create your own custom menu. Try the layout below and replace ic_launcher drawable with menu icons. Then just set onClickListeners to them and perform whatever functions you want to perform inside onClick method.
您可以采用线性布局,提供与操作栏看起来相同的背景,并放置您想要放置的任何图标,并使用onClickListener为它们提供功能。希望这会给你一些想法。这样您就可以创建自己的自定义菜单。尝试下面的布局,并用菜单图标替换ic_launcher drawable。然后只需将onClickListeners设置为它们,并执行要在onClick方法中执行的任何函数。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#66000000" >
<ImageView
android:id="@+id/xMenuBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/xMenuTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chats"
android:textSize="18sp"
android:layout_toRightOf="@+id/xMenuBtn1"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/xMenuBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/xMenuBtn3"
android:background="@drawable/ic_launcher" />
<ImageView
android:id="@+id/xMenuBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ic_launcher" />
</RelativeLayout>
#3
0
Try in your activity onCreate() :
试试你的活动onCreate():
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);
and then :
接着 :
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getSupportMenuInflater().inflate(R.menu.menu_activity, menu);
return super.onPrepareOptionsMenu(menu);
}
#1
71
You seem to be using the wrong menu: Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu
您似乎使用了错误的菜单:您的文件名为“menu_activity.xml”,并使用Resource-Id扩展菜单:R.menu.reminder_menu
The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity
菜单的资源名称应与文件名相同,即:R.menu.manu_activity
Try it with this again - I ran into this too once and it drove me nuts...
再试一次 - 我也碰到了这一次它让我疯了......
Update After clarification that the above part was for obfuscation, please make sure that:
更新在澄清上述部分是为了混淆之后,请确保:
- You extend ActionBarActivity.
- 您扩展了ActionBarActivity。
- You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
- 您为活动(或整个应用程序)使用(或扩展)Theme.AppCompat主题之一
- Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the
showAsAction
attribute, so that the compatibility library can pick them up. - 因为在较旧的设备上,不存在与Actionbar相关的属性,所以请确保XML中的所有这些属性都与自定义命名空间一起使用。这将是showAsAction属性,以便兼容性库可以拾取它们。
You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction
tag to app:showAsAction
according to the Android guide.
您已经定义了自定义命名空间(菜单标记中的“app”)。你需要根据Android指南将android:showAsAction标签更改为app:showAsAction。
Your corrected menu_activity.xml would then look like this:
您更正后的menu_activity.xml将如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_lang"
app:showAsAction="always"
android:title="@string/menu_lang"
android:icon="@android:drawable/ic_input_lang"/>
</menu>
The extended Android guide for actionbar covers these new and nasty traps...
动作栏的扩展Android指南涵盖了这些新的和讨厌的陷阱......
#2
5
Okay I think I found a solution for you. It is called Overflow Menu and you need to call the below method in your onCreate method.
好吧,我想我找到了适合你的解决方案。它被称为溢出菜单,您需要在onCreate方法中调用以下方法。
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this and let me know if it works for you.
试试这个,让我知道它是否适合你。
EDIT:
编辑:
I think I understood your problem finally. I can give you one idea. If you do not want to use overflow menu and just display menu items as displayed in the screen-shot you have posted, you can create a layout on top of your activity.
我想我终于理解了你的问题。我可以给你一个想法。如果您不想使用溢出菜单并只显示已发布的屏幕截图中显示的菜单项,则可以在活动之上创建布局。
You can take a linear layout, give a background that looks the same as action bar and place whatever icons you want to put there, and give functionality to them with onClickListener. Hope this will give you some idea. This way you can create your own custom menu. Try the layout below and replace ic_launcher drawable with menu icons. Then just set onClickListeners to them and perform whatever functions you want to perform inside onClick method.
您可以采用线性布局,提供与操作栏看起来相同的背景,并放置您想要放置的任何图标,并使用onClickListener为它们提供功能。希望这会给你一些想法。这样您就可以创建自己的自定义菜单。尝试下面的布局,并用菜单图标替换ic_launcher drawable。然后只需将onClickListeners设置为它们,并执行要在onClick方法中执行的任何函数。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#66000000" >
<ImageView
android:id="@+id/xMenuBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/xMenuTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chats"
android:textSize="18sp"
android:layout_toRightOf="@+id/xMenuBtn1"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/xMenuBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/xMenuBtn3"
android:background="@drawable/ic_launcher" />
<ImageView
android:id="@+id/xMenuBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ic_launcher" />
</RelativeLayout>
#3
0
Try in your activity onCreate() :
试试你的活动onCreate():
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);
and then :
接着 :
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getSupportMenuInflater().inflate(R.menu.menu_activity, menu);
return super.onPrepareOptionsMenu(menu);
}