Android V7支持库弹出菜单

时间:2013-07-31 15:48:52

标签: android android-support-library

我正在尝试使用支持V7库实现PopupMenu。所有编译都很好但是当我试着打电话时:

    PopupMenu popup = new PopupMenu(this, v);
    popup.getMenu().add(Menu.NONE,MENU_SHARE_A,1,R.string.A);
    popup.getMenu().add(Menu.NONE,MENU_SHARE_B,2,R.string.B);
    popup.show();

通话时出错:

  

07-31 17:23:53.365:E / AndroidRuntime(14128):   java.lang.RuntimeException:二进制XML文件行#17:您必须提供   layout_height属性。

我认为这个元素与“abc_popup_menu_item_layout.xml”相关:

<android.support.v7.internal.view.menu.ListMenuItemView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="?attr/dropdownListPreferredItemHeight"
        android:minWidth="196dip"
        android:paddingRight="16dip">

这是一个错误还是我做错了什么?

6 个答案:

答案 0 :(得分:18)

PopupMenu popup = new PopupMenu(this, v);

什么是“这个”指的是什么?第一个参数必须是像Activity这样的上下文,而不是ApplicationContext !!

答案 1 :(得分:14)

您可能想要检查清单文件中是否指定了适当的主题:

<application ... android:theme="@style/Theme.AppCompat">

答案 2 :(得分:1)

将此行添加到 manifest.xml

中的Activity
android:theme="@style/Theme.AppCompat"

答案 3 :(得分:0)

您的布局文件“abc_popup_menu_item_layout.xml”已经显示了已定义的高度。

“?attr / dropdownListPreferredItemHeight”来自不同的主题或库(例如ActionBarSherlock),或者错误来自不同的布局文件。

另外,如果换掉“?attr / dropdownListPreferredItemHeight”之类的“60dp”,错误会消失吗?

答案 4 :(得分:0)

@ style / Theme.AppCompat.Light为我解决了这个问题。 @ style / Theme.AppCompat是一个黑暗的主题,它不适合我的应用程序。

答案 5 :(得分:0)

还要检查manifest.xml中的每个活动中是否还有其他样式。这将覆盖应用程序标记中的Theme.AppCompat。

相关问题