android-fragments 如何设置DialogFragment的标题?

yhuiod9q  于 2022-11-14  发布在  Android
关注(0)|答案(7)|浏览(214)

这应该是一个简单的任务,但由于某种原因,我可以找到一种方法来设置DialogFragment的标题。(我正在使用onCreateView重载设置对话框内容)。
默认样式为标题留了一个位置,但我在DialogFragment类上找不到任何方法来设置它。
当使用onCreateDialog方法设置内容时,不知何故,标题被神奇地设置了,所以我想知道这是设计好的,还是在使用onCreateView重载时有一个特殊的技巧来设置它。

daupos2t

daupos2t1#

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

xqnpmsa82#

重写onCreateDialog并直接在Dialog上设置标题是否有效?如下所示:

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

tnkciper3#

Jason's answer曾经为我工作,但现在它需要下面的附加内容才能让标题显示。
首先,在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更改为其他内容,以便与您的主题的风格相匹配。

7cwmlq89

7cwmlq894#

DialogFragment可以表示为对话框和Activity请使用以下对这两种情况都能正常工作代码

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

zc0qhyus5#

你可以看一下official docs。我的做法是这样的:

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

w8biq8rn6#

类似于Ban Geoengineering's answer,但有一些修改,所以我在styles.xml中覆盖了DialogFragment使用的默认样式,而不是编码在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

xa9qqrwz

xa9qqrwz7#

如果使用视图绑定:

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

相关问题