Android PopupWindow和WRAP_CONTENT无法一起使用

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

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

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

Related of "Android PopupWindow和WRAP_CONTENT无法一起使用"

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

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

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

我希望能帮助你。

以下代码为我工作。

 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)

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

下面的代码是use-full用于减少布局宽度和高度popup窗口

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

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

快乐的编码朋友..

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

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

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

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

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

我有一个图像作为popup窗口的背景,我解决了把它作为布局的背景(我使用新的ConstraintLayout)与android:背景。 该图像是一个9-Patch图像,因此它以一种很好的方式resize。