简体   繁体   English

由 DialogFragment 制作的自定义对话框不显示对话框标题

[英]Custom dialog made from DialogFragment does not display the dialog title

I have created a custom error dialog fragment, The displayed dialog is able to display the error message and the button.我创建了一个自定义错误对话框片段,显示的对话框能够显示错误消息和按钮。 However, the title does not display on the dialog.但是,标题不会显示在对话框中。

class ErrorDialog(private val message: String): BaseMvcDialogFragment() {

    companion object {
        fun newInstance(message: String): ErrorDialog = ErrorDialog(message)
    }

    private lateinit var errorController : ErrorController
    private lateinit var  dialogEventBus: DialogEventBus

    override fun onCreate(savedInstanceState: Bundle?) {
        errorController = compositionRoot.errorController
        super.onCreate(savedInstanceState)
        dialogEventBus = compositionRoot.dialogEventBus
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val viewMvc = compositionRoot.viewMvcFactory.errorViewMvcImpl(null)
        val dialog = Dialog(requireContext())
        dialog.setContentView(viewMvc.getRootView())
        dialog.setTitle("Oops something went wrong....")
        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(false)
        errorController.bindView(viewMvc, message, dialogEventBus)
        return dialog
    }

    override fun onStart() {
        super.onStart()
        errorController.onStart()
    }

    override fun onStop() {
        super.onStop()
        errorController.onStop()
    }
}

The themes class主题 class

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.WeatherApp" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

显示的对话框

I have tried making changes to the themes, by adding a custom dialog theme and this did not seem to work我尝试通过添加自定义对话框主题来更改主题,但这似乎不起作用

It turns out that when you are working with a device that has an API greater than 23 this line default to true.事实证明,当您使用 API 大于 23 的设备时,此行默认为 true。

<item name="android:windowNoTitle">false</item>

To resolve this issue, I had to make some changes to my theme as seen below为了解决这个问题,我必须对我的主题进行一些更改,如下所示

<style name="Theme.WeatherApp" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:dialogTheme">@style/DialogTheme</item>
</style>

<style name="DialogTheme" parent="@style/Theme.MaterialComponents.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

You also need to make sure that you are inheriting from the same parent theme您还需要确保您继承自同一个父主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM