如何在Android中编辑PopUp菜单项的尺寸?

问题描述 投票:5回答:1

我有一个PopUp菜单,当我点击一个按钮时,它就会下拉。但是我觉得这个菜单中的项目与我的应用程序的整体视图相比并不好看。我想知道我是否可以编辑菜单中项目的尺寸。如果可能的话,也许可以把每个项目的高度变短。

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

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

popup.show();
android xml menu popupmenu
1个回答
0
投票

你不能自定义PopupMenu,所以使用弹出窗口代替。请找到下面的工作代码。

   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
© www.soinside.com 2019 - 2024. All rights reserved.