5

我有一个弹出菜单,当我单击按钮时会下拉。但是我觉得这个菜单中的项目相对于我的应用程序的整体视图看起来不太好。我想知道是否可以编辑菜单中项目的尺寸。如果可能的话,也许让每个项目的高度更短。

PopupMenu popup = new PopupMenu(this, settingsButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.settings_menu, popup.getMenu());

然后在按钮的 onClick() 方法中我调用 show()

popup.show();
4

1 回答 1

0

您无法自定义 PopupMenu,因此请改用 Popup 窗口。请找到以下工作代码。

   PopupWindow popup;

       LayoutInflater mInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = mInflater.inflate(R.layout.popuprow, null); //popup row is item xml

        layout.measure(View.MeasureSpec.UNSPECIFIED,
                    View.MeasureSpec.UNSPECIFIED);
            popup = new PopupWindow(layout, FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT,true);

     final TextView popupMenuitem = (TextView) layout.findViewById(R.id.popupTitle);
     // set click on listner on item where you can close the popup by dismiss()

    popup.showAsDropDown(filtericon,5,5); // filtericon is button name , clicking 
    which popup will launch.

    popup.dismiss();  // to close popup call this
于 2020-06-03T21:02:15.613 回答