【问题标题】:Android PopupWindow and WRAP_CONTENT don't work togetherAndroid PopupWindow 和 WRAP_CONTENT 不能一起工作
【发布时间】:2013-04-11 17:36:19
【问题描述】:

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

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

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

【问题讨论】:

  • 来自文档如果弹出窗口正在显示,则调用此方法将仅在下次显示弹出窗口时生效。这是你的问题吗?

标签: android android-layout layout popupwindow


【解决方案1】:

只需更改您创建它的方式:

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

【讨论】:

  • 这在内容大于锚点视图下方或上方的允许空间的情况下不起作用。或者,如果直到以后才测量内容。
【解决方案2】:

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

    【讨论】:

    • 这是唯一适用于我的解决方案。谢谢
    • 但需要 API >= 19
    【解决方案4】:
     final PopupWindow newPartWindow = new PopupWindow(newPartIconView, 1, 1, false);
        newPartWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    

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

    【讨论】:

      【解决方案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)

      【讨论】:

      • 这对我也有用。它不应该工作,因为 ListPopupWindow 与 PopupWindow 无关。我认为我们只是走运了——常量的值为-2,这给出了正确的行为,而 ViewGroup.LayoutParams.WRAP_CONTENT 的值为 -1。
      【解决方案6】:

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

      popview = LayoutInflater.from(context).inflate(R.layout.pop_citypicker, null);
      
      popwindow = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT,
              ViewGroup.LayoutParams.WRAP_CONTENT);
      

      【讨论】:

      • 对我来说简单的解决方案。将我的相对布局更改为线性并且它是固定的
      【解决方案7】:

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

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

      【讨论】:

        【解决方案8】:

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

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

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

        快乐的编码朋友..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多