【问题标题】:How to make a custom DialogFragment in Android?如何在 Android 中制作自定义 DialogFragment?
【发布时间】: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


    【解决方案1】:

    您没有创建任何Dialog,而是应该覆盖onCreateDialog

    class AddPlaylistPopUp: DialogFragment(){
    
        override fun onCreateDialog(
            savedInstanceState: Bundle?
        ): Dialog {
    
            val builder = AlertDialog.Builder(getContext())
            builder.setTitle("Title")
            builder.setView(R.layout.add_playlist_popup_layout)
    
            return builder.create()
        }
    }
    

    这样您就不需要实现onCreateView(LayoutInflater, ViewGroup, Bundle),并且您可以更好地控制对话框的创建。

    如果您想坚持使用onCreateView 方法,您可以通过将函数调用LayoutInflater.inflate(resource, root, attachToRoot) 的第三个参数设置为false 来解决您的问题。

    编辑: 保持match_parent的宽度,override Dialog size

    【讨论】:

    • attachToRoot = false,不幸的是没有效果。我尝试了 onCreateDialog,但它看起来有点愚蠢。正如您在我的对话框布局中看到的那样,对话框的宽度 = 匹配父级,但我在侧面得到了一些边距,您看到嵌套约束布局下应该有一些额外的空间,但它被裁剪掉了。
    • @DinuNicolae As of docs: "显示对话框窗口的片段,浮动在其活动窗口的顶部。该片段包含一个 Dialog 对象,它根据片段的状态。”。您没有在代码中创建任何对话框。也许DialogFragment 不适合您的用例?查看修改
    • 我只想要一个 100% 可燃的弹出窗口。实现这一目标的最佳方法是什么?
    • @DinuNicolae 好吧,AlertDialog 通常是推荐的方式。其他可能的方法仅取决于您的特定用例,这取决于您。请阅读文档以了解您可以对 AlertDialog 进行多少主题化和自定义
    • 感谢您的宝贵时间。我决定采取另一种方法。我只是使用了简单的对话框,我可以按照我想要的方式完成它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2014-10-23
    相关资源
    最近更新 更多