如何在 Android 中编辑弹出菜单项的尺寸? [英] How can I edit the dimensions of a PopUp Menu Item in Android?

查看:89
本文介绍了如何在 Android 中编辑弹出菜单项的尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a PopUp Menu that drops down when I click a button. However I feel the items in this menu don't look good with respect to the overall view of my App. I was wondering if I could edit the dimensions of the items in the Menu. Maybe make the height of each item shorter, if possible.

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

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

Then in the onClick() method of the button I call show()

popup.show();

推荐答案

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

You cant customize PopupMenu so use Popup window instead. PLease find the below working code.

   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

这篇关于如何在 Android 中编辑弹出菜单项的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆