如何设置对话框片段的标题?

这应该是一个简单的任务,但出于某种原因,我可以找到一种方法来设置一个 对话片段的标题。(我正在使用 onCreateView重载设置对话框内容)。

默认样式为标题保留了一个位置,但是我在 DialogFragment类中找不到任何方法来设置它。

当使用 onCreateDialog方法设置内容时,标题以某种方式神奇地被设置,所以我想知道这是否是设计好的,或者在使用 onCreateView重载时有一个特殊的技巧来设置它。

61379 次浏览

覆盖 onCreateDialog并直接在 Dialog上设置标题是否有效? 像这样:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("My Title");
return dialog;
}

你可以使用 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;
}
// ...
}

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>

经过几个小时尝试不同的东西,这是唯一一个已经做了我的把戏。

注意-你可能需要改变 Theme.AppCompat.Light.Dialog.Alert的其他东西,以配合您的主题风格。

你可以看看 官方文件。 我是这样做的:

@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();
}

对话框可以表示为对话框,也可以表示为活动

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getShowsDialog()) {
getDialog().setTitle(marketName);
} else {
getActivity().setTitle(marketName);
}
}

类似于 Ban Geoengineering 的回答,但是做了一些修改,所以我没有在 DialogFragment中编写要使用的特定主题,而是覆盖了 styles.xmlDialogFragment使用的默认样式。

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

如果使用视图绑定:

@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();
}