首页 > 解决方案 > 自定义主题未实现到 DialogFragment

问题描述

我在DialogFragment这里创建了一个扩展:

class AlertDialogFragment(context: Context) : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = AlertDialog.Builder(activity).apply {
            setStyle(STYLE_NO_FRAME, R.style.AlertDialogTheme)
            setNeutralButton("Cancel", object : DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {

                }
            })
            setPositiveButton("Replace", object : DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {

                }
            })
            setNegativeButton("Delete", object : DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {

                }
            })
        }
        return dialog.create()
    }

}

正如您在上面看到的,我已将 my 应用于AlertDialogTheme对话框:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:windowBackground">@color/colorPrimary</item>
</style>

但是,当显示我的对话框时:

override fun onClick(v: View) {
    if (v is AppCompatImageButton){
        val dialog = AlertDialogFragment(this)
        dialog.show(supportFragmentManager, "alertDialog")
    }
}

对话框的背景是黑色的(我认为是~#333)。我@color/colorPrimary是白色的,所以这就是背景的意思。

知道问题是什么吗?

标签: androidkotlin

解决方案


你可以那样做

class AlertDialogFragment : DialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container:ViewGroup?,savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
        var view = inflater.inflate(R.layout.fragment_dialog, container, false)

        view.delete.setOnClickListener {  }

        view.cancel.setOnClickListener {  }

        view.replace.setOnClickListener {  }

        return view
   }    

}

创建 xml 布局fragment_dialog并根据需要进行设计:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">


</LinearLayout>

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:orientation="horizontal">

     <Button
           android:id="@+id/cancel"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="cancel"
           android:layout_weight="1"/>

     <Button
           android:id="@+id/replace"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="replace"
           android:layout_weight="1"/>

     <Button
           android:id="@+id/delete"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="delete"
           android:layout_weight="1"/>

  </LinearLayout>

最后以您的自定义样式显示您的对话框,就像这样:

val dialog = AlertDialogFragment()
dialog.setStyle(DialogFragment.STYLE_NO_FRAME , R.style.AlertDialogTheme)
dialog.show(supportFragmentManager, "alertDialog")

推荐阅读