Android PopupWindow和WRAP_CONTENT无法一起使用

Bor*_*ris 18 layout android popupwindow android-layout

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

mInfoPopup = new PopupWindow(layout, 400, 600, true);
mInfoPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)

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

Gom*_*ino 31

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

PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
Run Code Online (Sandbox Code Playgroud)

  • 这在内容大于锚视图下方或上方的允许空间的情况下不起作用。或者,如果内容直到稍后才进行测量。 (2认同)

Ars*_*rst 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);
Run Code Online (Sandbox Code Playgroud)


小智 7

我发现了一个适合我的解决方案(我正在使用API​​ 10),你可以使用它:

popupWindow.setWindowLayoutMode(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);
Run Code Online (Sandbox Code Playgroud)

如果未设置高度/宽度或设置为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);

        }
    });
Run Code Online (Sandbox Code Playgroud)

我希望能帮助你.


Kri*_*a V 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);
Run Code Online (Sandbox Code Playgroud)

在这里,popup.setWidth(ListPopupWindow.WRAP_CONTENT) instead of popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)在我的情况下使用。