android中的弹出窗口大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3014118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup window size in android
提问by Bostjan
I'm creating a popup window in a listactivity in the event onListItemClick.
我在 onListItemClick 事件的 listactivity 中创建一个弹出窗口。
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pop = inflater.inflate(R.layout.popupcontact, null, false);
ImageView atnot = (ImageView)pop.findViewById(R.id.aNot);
height = pop.getMeasuredHeight();
width = pop.getMeasuredWidth();
Log.e("pw","height: "+String.valueOf(height)+", width: "+String.valueOf(width));
atnot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});
pw = new PopupWindow(pop, width, height, true);
// The code below assumes that the root container has an id called 'main'
//pw.showAtLocation(v, Gravity.CENTER, 0, 0);
pw.showAsDropDown(v, 10, 5);
Now, the height and width variables were supposed to be height and width of the layout used for the popup window (popupcontact). But they return 0. I guess that is because the layout isn't rendered yet. Does anyone have a clue, how can I control the size of the popup window without needing to use absolute pixel numbers?
现在,高度和宽度变量应该是用于弹出窗口 (popupcontact) 的布局的高度和宽度。但它们返回 0。我想这是因为布局尚未呈现。有没有人有线索,如何在不需要使用绝对像素数的情况下控制弹出窗口的大小?
采纳答案by YK Tan
I don't think your code will work because you are trying to get the width and height of the view that is a child of your popup window.
我认为您的代码不会起作用,因为您正试图获取作为弹出窗口子项的视图的宽度和高度。
Since you are anchoring the popupwindow to the "v" view, maybe set the width and height according to the anchor view (or at a ratio based on them)?
由于您将弹出窗口锚定到“v”视图,是否可以根据锚视图(或基于它们的比例)设置宽度和高度?
回答by Yann
I had a similar issue. I solved it by calling:
我有一个类似的问题。我通过调用解决了它:
pop.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
before getting the size of the layout with getMeasuredHeight() and getMeasuredWidth()
在使用 getMeasuredHeight() 和 getMeasuredWidth() 获取布局大小之前
回答by Janusz
If you want to create a Popup window take a look at the Creating Dialogs. If you want to use a complete own View in your dialog have a look at this questionmake sure to read the comments on the accepted answer. I think there is no need to use PopupWindow yourself to create this view and the dialog stuff will get you to a result much easier.
如果您想创建一个弹出窗口,请查看创建对话框。如果您想在对话框中使用完整的自己的视图,请查看此问题,请务必阅读已接受答案的评论。我认为没有必要自己使用 PopupWindow 来创建这个视图,对话框的东西会让你更容易得到结果。
If you want to stick with the PopupWindow and you don't want to change the width and height of your window just use another constructorfor PopupWindow there are some available that don't need height and width. They don't take a boolean for the ability to focus the window but it seems you can change this later on via setFocusable().
如果您想坚持使用 PopupWindow 并且不想更改窗口的宽度和高度,只需使用PopupWindow 的另一个构造函数,有一些不需要高度和宽度的可用构造函数。他们不使用布尔值来聚焦窗口,但您似乎可以稍后通过setFocusable()更改此设置。