Android PopupWindow和WRAP_CONTENT无法一起使用

时间:2013-04-18 14:30:54

标签: android android-layout layout popupwindow

我打开一个这样的popu窗口:

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

然后窗口获得指定的确切大小(400x600),并且不会将其大小调整为其内容。我需要更改什么才能使弹出窗口实际包裹其内容?

8 个答案:

答案 0 :(得分:31)

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

PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

答案 1 :(得分:9)

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);

答案 2 :(得分:7)

我找到了一个适合我的解决方案(我正在使用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);

        }
    });

我希望能帮助你。

答案 3 :(得分:4)

以下代码为我工作。

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)

答案 4 :(得分:1)

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

match_parent

答案 5 :(得分:1)

下面的代码是use-full用于减少布局宽度和高度pop-window

我使用的是具有对齐中心的自定义布局

  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);

这两行现在不使用完整

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

快乐的编码朋友..

答案 6 :(得分:0)

 final PopupWindow newPartWindow = new PopupWindow(newPartIconView, 1, 1, false);
    newPartWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

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

答案 7 :(得分:0)

我有类似的问题,但即使在新的PopupWindow(...)中设置大小也没有用。

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