我想创建一个像this link这样的弹出菜单,但是用于我的视图而不是操作栏。
我尝试使用android support library v7来支持旧设备。问题是我在创建后端口弹出菜单时得到了RuntimeException: You must supply a layout_height attribute
。
这是我的代码段:
// use support lib
import android.support.v7.widget.PopupMenu;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button);
// Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu,
popup.getMenu());
// registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
logcat错误:
03-18 16:39:53.005: E/AndroidRuntime(28936): FATAL EXCEPTION: main
03-18 16:39:53.005: E/AndroidRuntime(28936): java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:492)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6057)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6225)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:610)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:554)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:56)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.view.LayoutInflater.inflate(LayoutInflater.java:486)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:335)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:190)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:128)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:102)
03-18 16:39:53.005: E/AndroidRuntime(28936): at android.support.v7.widget.PopupMenu.show(PopupMenu.java:108)
03-18 16:39:53.005: E/AndroidRuntime(28936): at com.javatpoint.popupmenu.MainActivity$1.onClick(MainActivity.java:41)
如何解决此错误以及如何将弹出菜单的自定义样式应用于支持库?
更新:根据this answer将我的主题更改为Theme.AppCompat
,此错误已修复。但我仍然不知道如何为弹出菜单定制样式
任何帮助,将不胜感激!