显示带有NavHostFragment的DialogFragment显示为空



Im正在尝试显示一个DialogFragment,该对话框出现在屏幕上后,应显示NavHostFragment中指定的nav_graph的起始目的地。这是DialogFragment布局:

<?xml version="1.0" encoding="utf-8"?>
<layout>
<!--    <androidx.constraintlayout.widget.ConstraintLayout -->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="800dp">-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:visibility="visible">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:orientation="vertical">
<fragment
android:id="@+id/nav_host_home_bottom_sheet"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/dismiss_registration_button"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/membership_navigation" />
<com.google.android.material.button.MaterialButton
android:id="@+id/dismiss_registration_button"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/open_sans_regular"
android:text="@string/all_dismiss"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!--    </androidx.constraintlayout.widget.ConstraintLayout>-->

所发生的情况是对话框片段中只显示Dismiss按钮。当我取消对layout_height固定为800dp的ConstraintLayout的注释时,从NavHostFragment启动Destination加载正常。问题是我不想对layout_height进行硬编码-如何在不硬编码layout_height的情况下用CoordinatorLayoutou或ConstraintLayout显示它?

为了让它发挥作用,我只需将DialogFragment布局更改为

<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/membership_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

然后在需要的地方处理滚动视图和其他片段。

最新更新