gpt4 book ai didi

android - DialogFragment 已触发,但未在 Navigation Architecture 组件中显示布局

转载 作者:行者123 更新时间:2023-12-05 06:14:09 26 4
gpt4 key购买 nike

在我的一个应用程序中,我使用了 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>

这是对话框 fragment 的基类

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 替换为 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")
}

关于android - DialogFragment 已触发,但未在 Navigation Architecture 组件中显示布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63007406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com