类似于 Ban Geoengineering 的回答,但是做了一些修改,所以我没有在 DialogFragment中编写要使用的特定主题,而是覆盖了 styles.xml中 DialogFragment使用的默认样式。
在 androidx.fragment.app.DialogFragment中设置标题。
class EditBatteryLevelFragment:DialogFragment(),SelfClosingFragment.Host
{
override fun onCreateView(
inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?
):View
{
// set dialog title
requireDialog().setTitle(R.string.edit_battery_level__title)
// .....
return someView
}
}
<!-- ... -->
<!-- define the style for your dialog -->
<style name="AppTheme.Dialog" parent="ThemeOverlay.MaterialComponents.Dialog">
<!-- add a minimun width to the dialog, so it's not too narrow -->
<item name="android:windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>
<!-- display the title for dialogs -->
<item name="android:windowNoTitle">false</item>
</style>
</resources>