这应该是一个简单的任务,但出于某种原因,我找不到设置DialogFragment标题的方法。(我正在使用onCreateView
重载函数设置对话框内容。)
默认样式会留出标题的位置,但是我在DialogFragment
类中找不到任何设置标题的方法。
当使用onCreateDialog
方法设置内容时,标题会以某种神奇的方式自动设置,因此我想知道这是否是设计上的缺陷,或者在使用onCreateView
重载函数时是否有特殊的技巧可以设置它。
这应该是一个简单的任务,但出于某种原因,我找不到设置DialogFragment标题的方法。(我正在使用onCreateView
重载函数设置对话框内容。)
默认样式会留出标题的位置,但是我在DialogFragment
类中找不到任何设置标题的方法。
当使用onCreateDialog
方法设置内容时,标题会以某种神奇的方式自动设置,因此我想知道这是否是设计上的缺陷,或者在使用onCreateView
重载函数时是否有特殊的技巧可以设置它。
getDialog().setTitle("My Dialog Title")
来设置对话框的标题。就像这样:public static class MyDialogFragment extends DialogFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("My Dialog Title");
View v = inflater.inflate(R.layout.mydialog, container, false);
// ...
return v;
}
// ...
}
重写onCreateDialog
并直接在Dialog
上设置标题可以吗?像这样:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("My Title");
return dialog;
}
Jason的回答 曾经对我有用,但现在需要添加以下内容以使标题显示。
首先,在你的 MyDialogFragment 的 onCreate()
方法中,添加:
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
然后,在你的 styles.xml 文件中添加:
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
尝试了几个小时不同的方法,这是唯一一个对我有用的。
NB - 您可能需要将Theme.AppCompat.Light.Dialog.Alert
更改为其他内容,以匹配您主题的样式。
Android.Support.V4.App.DialogFragment
切换到Android.Support.V7.App.AppCompatDialogFragment
后,设置android:windowNoTitle
为*false
*是不必要的,也不足以解决问题,因为这会导致重复的标题(即使我确保它只被设置一次)。 - samusDialogFragment可以表示为对话框和活动。使用下面的代码,可以为两者都正确地工作
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getShowsDialog()) {
getDialog().setTitle(marketName);
} else {
getActivity().setTitle(marketName);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("My Title");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_layout, null);
builder.setView(view);
return builder.create();
}
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
}
}
styles.xml
中,覆盖android:dialogTheme
,这是由DialogFragment
实例使用的默认样式。<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents">
<!-- BONUS READING: override material colors here, too https://material.io/develop/android/theming/color -->
<!-- override DialogFragment theme of those spawned by activities with this theme -->
<item name="android:dialogTheme">@style/AppTheme.Dialog</item>
</style>
<!-- ... -->
styles.xml
中,还需要声明对话框主题,该主题将被 DialogFragment
实例使用。这个样式从 ThemeOverlay
继承非常重要,这样它将保留您应用程序的主题颜色。 <!-- ... -->
<!-- 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>
请确保生成 DialogFragment
的活动使用了定义的 AppTheme
。
<item name="android:windowNoTitle">false</item>
来显示标题并使对话框全屏。 - nhcodes@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
binding = YourDialogXmlBinding.inflate(getLayoutInflater());
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle("Your title here")
.setView(binding.getRoot());
return builder.create();
}