如何获取警报对话尺寸?

发布于 2025-02-03 09:42:40 字数 970 浏览 2 评论 0原文

我正在尝试使用“警报”对话框(例如底部纸上的弹出视图)创建自定义工具提示。在这样做的过程中,我试图使用窗口属性将其定位在特定的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中测量了父布局之后,它也会返回其他

几种情况,

  1. arperdialog.window?attributes.width.width& amp; alertdialog.window?.Attributes.height返回 -2

  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,

  1. alertDialog.window?.attributes.width & alertDialog.window?.attributes.height returns -2

  2. Measuring 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-02-10 09:42:40

我一直在寻找如何获得对话的高度。
根据此答案, https://stackoverflow.com/a/57104421 注册layoutchangelistener。

您可以使用类似的东西:

val alertDialog = AlertDialog.Builder(parent.context)
    .setView(R.layout.sample_layout)
    .create()
alertDialog.window?.decorView?.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ ->
    val alertBoxHeight = v.height
    val alertBoxWidth = v.width
}
alertDialog.show()

这是它对我有用的唯一解决方案。

我希望它也能帮助您!!!

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 :

val alertDialog = AlertDialog.Builder(parent.context)
    .setView(R.layout.sample_layout)
    .create()
alertDialog.window?.decorView?.addOnLayoutChangeListener { v, _, _, _, _, _, _, _, _ ->
    val alertBoxHeight = v.height
    val alertBoxWidth = v.width
}
alertDialog.show()

It was the only solution that it worked for me.

I hope it is going to help you too!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文