android - 自定义对话框需要时间才能显示

标签 android performance dialog customdialog

当我使用具有相当复杂布局的 AlertDialog.Builder 或 DialogFragment(如 this 之一)实现自定义对话框时,对话框需要一些时间才能显示出来。但是当对话框只包含几个 editText 左右时......显示对话框没有问题。为什么会这样?
是不是 AlertDialog 框架用于构建仅用于提醒用户的对话框? (顾名思义。)
是否有其他一些框架可用于实现复杂的自定义对话框?
我尝试了以下方法来解决此问题:

  • 试图实现线程,以便我的对话框在后台准备好
    线程并在我想要的时候显示它。但这一般是不允许的
    因为除了 UI 线程之外的任何其他线程都不应该接触 UI
    相关事件。
  • 使对话框成为全局变量,在 onCreate 中初始化它并
    然后显示对话框 onButtonClick。
  • 切换到约束布局
  • 通过将对话框主题设置为
    list 文件中的 Activity 。

  • 我正在扩展至 this问题。
    任何帮助将不胜感激,在此先感谢。

    最佳答案

    AlertDialogDialogFragment框架很慢,因为它们需要一些时间来进行计算和 fragment 化。所以解决这个问题的方法是,直接使用 Dialog 框架。

  • 使用 Dialog框架的构造函数来初始化一个对话框对象,如下所示:
    Dialog dialog = new Dialog(context, R.style.Theme_AppCompat_Dialog);
    // the second parameter is not compulsory and you can use other themes as well
    
  • 定义布局,然后使用 dialog.setContentView(R.layout.name_of_layout) .
  • 使用dialog.findViewById(R.id.name_of_view)从对话框的布局文件中引用 View

  • 然后像任何人在 Activity 类中所做的那样实现逻辑。通过阅读官方 documentation 找出适合您用例的最佳实现.

    关于android - 自定义对话框需要时间才能显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63373221/

    相关文章:

    arrays - Swift Array firstIndex 在匹配 2 个小型结构数组时需要 20 秒

    javascript - 允许在 jQuery Mobile 对话框中使用脚本?

    android - android API 11 中的空 NumberPicker

    android - Android 应用程序中自定义 ImageView 的 ClassNotFoundException

    jquery选择器性能

    iphone - OpenGL : Clipping textures and its performance

    java - Netbeans GUI 预览不同于运行时 View

    尝试设置默认应用程序时 Android 设置崩溃(基于主机的卡模拟)

    android - 具有自定义 View 的 AlertDialog.Builder : how to initialize the view?

    Android 检查某些平板电脑上的网络连接崩溃