【发布时间】:2020-03-28 10:36:14
【问题描述】:
我正在尝试从片段中显示自定义对话框。当它即将显示时,屏幕变暗,但没有显示对话框。我也尝试从包含片段的活动中显示它,但我得到了相同的结果。
这是我的对话框的布局:
<?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"
android:layout_width="match_parent"
android:layout_height="239dp"
android:background="#00f"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/pop_up_shape"
android:backgroundTint="#00ff00"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的对话框片段类:
class AddPlaylistPopUp: DialogFragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.add_playlist_popup_layout, container, true)
}
}
这是应该在活动中打开对话框的方法:
fun testBut(view: View) {
val fm: FragmentManager = supportFragmentManager
val editNameDialogFragment = AddPlaylistPopUp()
editNameDialogFragment.show(fm, "fragment_edit_name")
}
在我这样尝试的片段中:
fun openDialog(){
val fm = fragmentManager!!
val editNameDialogFragment = AddPlaylistPopUp()
editNameDialogFragment.show(fm, "fragment_edit_name")
}
这是我遵循的指南: https://guides.codepath.com/android/Using-DialogFragment#passing-data-to-parent-fragment
【问题讨论】:
标签: android kotlin android-dialogfragment dialogfragment