Android DialogFragment 标题未显示

Android DialogFragment title not showing

创建自定义 DialogFragment 时,我使用以下设置标题:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_dialog_add, container, false);      

    // Setting title here
    getDialog().setTitle("Add New");

    return v;
}

上面的代码对我 23 岁以上的 API 级别工作正常。对于 API 23,标题根本不显示。

知道为什么吗?以及如何让标题在 API 23 上显示?

通过将以下内容添加到 styles.xml 中解决:

<item name="android:dialogTheme">@style/CustomDialog</item>

<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

你的风格:

<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

如果您的对话框是片段:

MyFragment myFragment = new MyFragment();
myFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);

我使用 .setMessage 而不是 .setTitle 解决了问题。我知道它的外观不一样,但就我而言,我只需要给用户一个提示。