21

我打开一个这样的弹出窗口:

mInfoPopup = new PopupWindow(layout, 400, 600, true);
mInfoPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

然后窗口会获得指定的确切大小 (400x600),并且不会根据内容调整其大小。我需要更改哪些内容才能使弹出窗口真正环绕其内容?

4

8 回答 8

35

只需通过以下方式更改您创建它的方式:

PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
于 2013-07-02T16:36:08.923 回答
13
PopupWindow popupWin = new PopupWindow(mContext);
// WRAP_CONTENT works well for height, but not for width.
popupWin .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 
// Measure layout here, then we can get measureHeight.
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWin .setWidth(layout.getMeasuredWidth()); 
popupWin .setContentView(layout);
popupWin .showAsDropDown(anchorView,0 ,0, Gravity.NO_GRAVITY);
于 2015-06-19T02:28:46.113 回答
8

我找到了一个适合我的解决方案(我正在使用 API 10),您可以使用它:

popupWindow.setWindowLayoutMode(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);

如果您不设置高度/宽度或设置 0,它将不起作用。例子:

...
private Button button;

protected void onCreate(Bundle savedInstanceState) {
    ...
    attached_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {                
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup_layout, null);

            final PopupWindow popupWindow = new PopupWindow(getApplicationContext());
            popupWindow.setContentView(popupView);
            popupWindow.setWindowLayoutMode(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(1);
            popupWindow.setWidth(1);
            popupWindow.setFocusable(true);

            Button dismiss = (Button) popupView
                    .findViewById(R.id.dismiss);

            dismiss.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(button);

        }
    });

我希望能帮助你。

于 2015-02-18T10:22:20.510 回答
5

以下代码对我有用。

LayoutInflater layoutInflater = LayoutInflater.from(context);
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(ListPopupWindow.WRAP_CONTENT);
popup.setHeight(ListPopupWindow.WRAP_CONTENT);

在这里,popup.setWidth(ListPopupWindow.WRAP_CONTENT) instead of popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)在我的案例中使用。

于 2016-05-06T16:14:12.577 回答
3

不要在布局中使用RelativeLayout,替换LinearLayout,也许它正在工作。

popview = LayoutInflater.from(context).inflate(R.layout.pop_citypicker, null);

popwindow = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
于 2016-09-08T06:12:56.187 回答
1

下面的代码可用于减少布局宽度和高度弹出窗口

我在那个自定义布局中使用了对齐的中心

  final PopupWindow popupWindow = new PopupWindow(getActivity());
            PopupMenu popup = new PopupMenu(getActivity(), v, Gravity.CENTER);
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.popmenu1t1, null);
            l1 = (LinearLayout) view.findViewById(R.id.atoz);
            l2 = (LinearLayout) view.findViewById(R.id.ztoa);
            l3 = (LinearLayout) view.findViewById(R.id.phtol);
            l4 = (LinearLayout) view.findViewById(R.id.pltoh);
            l5 = (LinearLayout) view.findViewById(R.id.dhtol);
            l6 = (LinearLayout) view.findViewById(R.id.dltoh);
            l7 = (LinearLayout) view.findViewById(R.id.dotor);
            l8 = (LinearLayout) view.findViewById(R.id.drtoo);
            int width = 900;
            int height = 400;
            try {
                WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
                width = wm.getDefaultDisplay().getWidth();
                height = wm.getDefaultDisplay().getHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }

            popupWindow.setWidth(width*3/6);
            popupWindow.setHeight(height*3/8);
            popupWindow.setFocusable(true);

            popupWindow.setContentView(view);
            popupWindow.setBackgroundDrawable(null);
            popupWindow.setOutsideTouchable(true);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

这两行暂时没有使用 full

popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                 popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

快乐的编码朋友..

于 2017-03-01T02:54:43.657 回答
0
 final PopupWindow newPartWindow = new PopupWindow(newPartIconView, 1, 1, false);
    newPartWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

这对我有用,我们需要将宽度/高度初始化为 1 以使包装内容起作用。

于 2016-03-08T22:42:39.810 回答
0

我遇到了类似的问题,但即使在 new PopupWindow(...) 中设置大小也不起作用。

我有一个图像作为弹出窗口的背景,我解决了将它作为布局的背景(我正在使用新的 ConstraintLayout)和 android:background。该图像是一个 9-Patch 图像,因此它可以很好地调整大小。

于 2016-12-31T14:14:46.970 回答