DialogFragment 已触发,但未显示导航架构组件中的布局

2024-01-10

在我的一个应用程序中,我使用 Android 导航架构组件。一切都很顺利。但当我想导航到 DialogFrgment 时,我陷入了困境。虽然触发了DialogFragment,但没有看到布局。只能看到重叠模糊背景。无法弄清楚实际问题。这是我用来显示dialogFragment的代码。

这是我想展示的自定义布局

terms_and_condition.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:ignore="WebViewLayout">

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_60sdp"
        android:background="@color/colorAccent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:weightSum="4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_20sdp"
            android:layout_weight="3"
            android:text="@string/terms_amp_conditions"
            android:textSize="@dimen/_15ssp"
            android:textStyle="bold"
            android:textColor="@color/white" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/close_privacy_policy_dialog_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_close" />

    </LinearLayout>

    <WebView
        android:id="@+id/privacy_policy_web_view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_300sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout6"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是对话框片段的基类

BaseDialogFragment.kt

abstract class BaseDialogFragment : DaggerAppCompatDialogFragment() {

    @LayoutRes
    abstract fun getLayoutResource(): Int

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return container?.inflate(getLayoutResource())
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.let {
            it.requestFeature(FEATURE_NO_TITLE)
            it.setBackgroundDrawableResource(R.color.transparent)
        }
        return dialog
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        dialog?.window?.setLayout(
            MATCH_PARENT,
            WRAP_CONTENT
        )

    }

    protected fun disableDismissOnBackPress() {
        dialog?.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KEYCODE_BACK) {
                if (event.action != ACTION_DOWN) true else true
            } else {
                false
            }
        }
    }

}

TermsAndConditionDialogFragment.kt

class TermsAndConditionDialogFragment: BaseDialogFragment() {

    override fun getLayoutResource() = R.layout.dialog_terms_and_condition

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val closeTheDialogImageView = view.findViewById<ImageView>(R.id.close_privacy_policy_dialog_image_view)
        val privacyPolicyWebView = view.findViewById<WebView>(R.id.privacy_policy_web_view)
        privacyPolicyWebView.loadUrl("file:///android_asset/privacy_policy.html")

        closeTheDialogImageView.setOnClickListener { dialog?.dismiss() }
    }

}

navigation_graph.xml

<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/splashFragment">
    <dialog
        android:id="@+id/termsAndConditionDialogFragment"
        android:name="com.aomi.mybase.ui.feature.termAndCondition.TermsAndConditionDialogFragment"
        tools:layout="@layout/dialog_terms_and_condition"/>


</navigation>

SignUpFragment.kt

terms_and_condition_button.setOnClickListener {
    findNavController().navigate(R.id.termsAndConditionDialogFragment)
}

改变你的onCreateDialog像下面一样并替换你的DaggerAppCompatDialogFragment to DialogFragment

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return this.activity?.let {
      val builder = AlertDialog.Builder(it)
      val inflater = requireActivity().layoutInflater

      val layoutId: Int = R.layout.your_layout

      val rootView = inflater.inflate(layoutId, null)
      builder.setView(rootView)

      val alertDialog = builder.create()
      alertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
      alertDialog
    } ?: throw IllegalStateException("Activity cannot be null")
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DialogFragment 已触发,但未显示导航架构组件中的布局 的相关文章

随机推荐

  • 获取中间件中的路由定义

    有谁知道是否可以获取用于触发路线的路径 例如 假设我有这个 app get user id function req res 使用以下简单的中间件 function req res next req 我希望能够得到 user id在中间件中
  • Instagram Streaming API 的 Logstash 输入插件

    我想阅读 Instagram 上的活动 我想知道我是否可以使用 Logstash 来做到这一点 类似于使用 Twitter 输入插件从 Twitter 读取事件 但 Instagram 没有输入插件 还有其他方法可以使用 Logstash
  • Pygame 中自上而下的运动

    如果这个问题之前已经被问过 我很抱歉 但我到处都检查过 但找不到答案 如何在 pygame 中进行自上而下的移动 如果我只使用矩形 这会很容易 但我将使用单个字符精灵 例如 如果我按d为了让玩家向右走 它会向我显示他向右走的角色精灵并将角色
  • Android KitKat 中的 WebView 渲染问题

    我一直在开发一个具有 WebView 的应用程序 其中从资产加载静态页面 也使用 JavaScript 此 WebView 在 KitKat 中不起作用 它保持空白 我知道在 kitkat 的 WebView 中发生的渲染引擎 webkit
  • Azure 存储模拟器无法初始化,并显示“数据库‘AzureStorageEmulatorDb57’不存在”

    我在使用 Azure 存储模拟器时遇到问题 我尝试重新初始化数据库并收到以下错误 这是在安装 Visual Studio 2019 预览版之后发生的 但这可能只是巧合 我尝试了一个小时左右让它运行 然后放弃了 只是使用 保留我的文件 选项重
  • 减少功能如何工作?

    据我了解 reduce 函数需要一个列表l和一个函数f 然后 它调用该函数f列表的前两个元素 然后重复调用该函数f与下一个列表元素和上一个结果 因此 我定义了以下函数 以下函数计算阶乘 def fact n if n 0 or n 1 re
  • 该模型没有从输入中返回损失 - LabSE 错误

    我想使用小队数据集微调 LabSE 以进行问答 我收到这个错误 ValueError The model did not return a loss from the inputs only the following keys last
  • Cassandra Nodejs DataStax 驱动程序不会通过准备好的语句执行返回新添加的列

    在架构中添加一对列后 我想通过以下方式选择它们select 反而select 返回旧的列集并且没有新的列 根据文档推荐 我使用 prepare true 来平滑 JavaScript 浮点数和 Cassandra ints bigints
  • 如何使用温莎城堡实例化基于 web.config 文件的类?

    我正在开发一个 ASP NET MVC 应用程序 我想根据 web config 中编写的设置实例化一个类
  • 如何在 Django 中使用单个查询集选择记录并更新它?

    我如何运行update and select相同的声明queryset而不必执行两个查询 一项选择对象 和一个更新对象 SQL 中的等效内容类似于 update my table set field 1 some value where p
  • 如何使用正则表达式格式化数字[重复]

    这个问题在这里已经有答案了 我遇到了涉及格式化数字的问题 在巴西 我们有一种称为 CPF 的文件 这是每个公民都拥有的一种个人身份证 以下是格式正确的 CPF 号码示例 096 156 487 09 我正在尝试使用正则表达式来格式化包含 C
  • ‘php.exe’不被识别为内部或外部命令、可运行程序或批处理文件

    php exe 不被识别为内部或外部命令 可运行的程序或批处理文件 即使我已将 PHP 添加到环境变量中 为什么仍会出现该错误 我的环境变量PATH如下所示 C Program Files NVIDIA Corporation PhysX
  • 如何在8086汇编中减去两个64位整数

    编写一个名为 SUB64 的程序 用 0x0160 和 0x0164 中的 64 位整数减去内存位置 0x0150 和 0x0154 中的 64 位整数 将结果存储在内存位置 0x0170 和 0x0174 中 我理解将其分成更小的部分背后
  • python re.findall() 与交替的子字符串

    如果我在正则表达式交替中有另一个字符串或模式的子字符串 或 子模式 如下所示 r abcd bc 预期的行为是什么re compile r abcd bc findall abcd bcd bc ab 尝试一下 我得到了 正如预期的那样 a
  • 使用 Square Connect API 访问客户信息

    是否可以使用 Square Connect API 访问有关商家客户的任何信息 最理想的信息是客户输入的收据电子邮件地址 但某种类型的唯一客户 ID 可以很好地确定回头客 纵观整个Square Connect API 文档 https co
  • 为什么通过提取方法进行重构会触发借用检查器错误?

    我的网络应用程序的架构可以简化为以下内容 use std collections HashMap Represents remote user Usually has fields but we omit them for the sake
  • UIStackview 左对齐

    我试图将 UIStackview 中的项目水平向左对齐 有没有办法以编程方式执行此操作 我尝试通过故事板来做到这一点 但没有成功 由于某种原因 UIStackView 中的项目会自行居中 如果您使用 StackView 将 axis 属性设
  • ASP.Net 应用程序作为 IIS_USR 执行 powershell 脚本

    我正在构建一个 asp net mvc 应用程序 它将作为我们编写的许多 powershell 脚本的包装器来运行 以管理日常任务 最终目标是让非技术人员轻松使用这些脚本 我已经设法让脚本很好地执行 var ctx System Web H
  • 用C++实现二阶低通滤波器,如何计算系数?

    我正在努力寻找合适的算法来生成低通滤波器的系数 我写了以下内容butterworthLowPass代码来自another https stackoverflow com a 20932062 2612235那么问题 class Filter
  • DialogFragment 已触发,但未显示导航架构组件中的布局

    在我的一个应用程序中 我使用 Android 导航架构组件 一切都很顺利 但当我想导航到 DialogFrgment 时 我陷入了困境 虽然触发了DialogFragment 但没有看到布局 只能看到重叠模糊背景 无法弄清楚实际问题 这是我