【问题标题】:How to set the title of DialogFragment?如何设置DialogFragment的标题?
【发布时间】:2011-07-08 19:13:10
【问题描述】:

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

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

当使用onCreateDialog 方法设置内容时,标题以某种方式神奇地设置,所以我想知道这是设计使然,还是在使用onCreateView 重载时设置它的特殊技巧。

【问题讨论】:

    标签: android android-fragments android-dialogfragment


    【解决方案1】:

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

    【讨论】:

    • 是的,确实 getDialog().setTitle() 可以解决问题。在查看了解决方案之后,以这种方式工作确实很有意义,但由于某种原因,我希望将标题设置为 DialogFragment 类本身的调用(定义 setStyle() 和 DialogFragment.STYLE_NO_TITLE 的位置相同)。非常感谢您的解决方案。
    • 如果我想改变标题的背景怎么办。这可能吗?
    • 另外请注意,您不能在片段的 onCreate() 方法中设置对话框标题,因为对话框尚未创建:)
    • @Rob Holmes 我想从对话框类(我创建实例的地方)之外设置标题。 IE。 MyDialogFragment myFragment = new MyDialogFragment(); myFragment.getDialog().setTitle("myTitle"); myFragment.show(getFragmentManager(), "myTitle");但是,此时 getDialog() 似乎返回 null。是否有另一种方法可以从这里设置标题(不制作我自己的 setTitle 方法)?
    • 我相信 Jason 的方法在一般情况下更正确。
    【解决方案2】:

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

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

    【讨论】:

    • 您的解决方案更好,因为片段不仅可以在对话框中使用
    • 这个解决方案对 xamarin.android 也很有用
    • 另请参阅我基于上述代码的最新答案:stackoverflow.com/a/41798042/1617737
    【解决方案3】:

    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>
    

    经过数小时尝试不同的事情,这是唯一一个为我成功的人。

    注意 - 您可能需要将 Theme.AppCompat.Light.Dialog.Alert 更改为其他名称以匹配您的主题风格。

    【讨论】:

    • 设置android:windowNoTitle false 在从Android.Support.V4.App.DialogFragment 切换到Android.Support.V7.App.AppCompatDialogFragment 后没有必要,也不够,因为它会导致重复的标题(尽管我确保它只设置了一次)。
    【解决方案4】:

    DialogFragment 可以表示为对话框和活动。使用下面的代码可以正常工作

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

    【讨论】:

      【解决方案5】:

      您可以查看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();
      }
      

      【讨论】:

        【解决方案6】:

        类似于Ban Geoengineering's answer,但有一些修改,因此我没有编写DialogFragment 中使用的特定主题,而是覆盖DialogFragments 在我的styles.xml 中使用的默认样式。

        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

        【讨论】:

        • 完美答案!我只需要&lt;item name="android:windowNoTitle"&gt;false&lt;/item&gt; 来显示标题并使对话框全宽。
        猜你喜欢
        • 2013-06-11
        • 2023-03-31
        • 2021-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多