Android V7 Support Library Popup Menu

我的未来我决定 提交于 2019-12-17 18:57:07

问题


I'm trying to implement a PopupMenu with the Support V7 Library. All compiles fine but when I try to call:

    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();

an error occurs on call:

07-31 17:23:53.365: E/AndroidRuntime(14128): java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.

Which refers I think to "abc_popup_menu_item_layout.xml" with this element:

<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">

Is this is a bug or do I do something wrong?


回答1:


You might want to check if you have an appropriate theme specified in your manifest file:

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



回答2:


PopupMenu popup = new PopupMenu(this, v);

what's "this" refer to ? the first parameter must be a context like Activity, not ApplicationContext!!




回答3:


Add this line to your Activity inside your manifest.xml

android:theme="@style/Theme.AppCompat"



回答4:


Your layout file "abc_popup_menu_item_layout.xml" already shows a height defined.

Either "?attr/dropdownListPreferredItemHeight" is something from a different theme or library (such as ActionBarSherlock), or the error is coming from a different layout file.

Also, if you swap out "?attr/dropdownListPreferredItemHeight" for something like "60dp" does the error go away?




回答5:


@style/Theme.AppCompat.Light resovled the problem for me. @style/Theme.AppCompat is a dark theme, it deosn't fit my applicatoin.




回答6:


Also check if there is another style that is applied within each activities in manifest.xml. This will override Theme.AppCompat in application tag.



来源:https://stackoverflow.com/questions/17975002/android-v7-support-library-popup-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!