自定义主题未实现到DialogFragment

时间:2019-06-06 22:23:18

标签: android kotlin

我在此处创建了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()
    }

}

如上所示,我已将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是白色的,所以背景就是这个意思。

有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您可以那样做

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")