如何获取警报对话尺寸?
我正在尝试使用“警报”对话框(例如底部纸上的弹出视图)创建自定义工具提示。在这样做的过程中,我试图使用窗口属性将其定位在特定的X和Y上。我需要测量我的警报对话框的宽度和高度。
val alertDialog = AlertDialog.Builder(parent.context)
.setView(R.layout.sample_layout)
.create()
alertDialog.show()
val alertBoxHeight = alertDialog.window?.decorView?.height
val alertBoxWidth = alertDialog.window?.decorView?.width
以上返回0。 即使在r.layout.sample_layout
中测量了父布局之后,它也会返回其他
几种情况,
arperdialog.window?attributes.width.width
& amp;alertdialog.window?.Attributes.height
返回 -2测量警报对话框的内容几乎具有正确的高度,但宽度很大(大于父母)
val alertlayout = alertdialog.findviewbyid< bentaintlayout>(r.id.parentlayout) AlertLayout?.measure(view.measurespec.makemeasurespec(0,view.measurespec.unspecified),view.measurespec.makemeasurespec(0,view.measurespec.unspec.unspecified))
I am trying to create a custom tooltip using the Alert dialog (Like a pop-up view over a bottom sheet). While doing so, I am trying to position it to a particular x and y using window attributes. I will need to measure my alert dialog's width and height.
val alertDialog = AlertDialog.Builder(parent.context)
.setView(R.layout.sample_layout)
.create()
alertDialog.show()
val alertBoxHeight = alertDialog.window?.decorView?.height
val alertBoxWidth = alertDialog.window?.decorView?.width
The above returns 0.
Even after measuring the parent layout in R.layout.sample_layout
, it returns the same
Few other cases,
alertDialog.window?.attributes.width
&alertDialog.window?.attributes.height
returns -2Measuring the content of alert dialog gives almost correct height but width is very large (greater than parent)
val alertLayout = alertDialog.findViewById<ConstraintLayout>(R.id.parentLayout) alertLayout?.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我一直在寻找如何获得对话的高度。
根据此答案, https://stackoverflow.com/a/57104421 注册layoutchangelistener。
您可以使用类似的东西:
这是它对我有用的唯一解决方案。
我希望它也能帮助您!!!
I was looking for long time how to get the dialog's height.
According to this answer https://stackoverflow.com/a/57104421 you can get it (you can get also the width) by registering a layoutChangeListener.
You can use something like :
It was the only solution that it worked for me.
I hope it is going to help you too!!!