【问题标题】:Android DialogFragment title not showingAndroid DialogFragment 标题未显示
【发布时间】:2015-09-04 19:10:13
【问题描述】:

创建自定义 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;
}

以上代码在 API 级别超过 23 的情况下可以正常工作。对于 API 23,标题根本没有显示。

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

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案1】:

    通过将以下内容添加到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>
    

    【讨论】:

    • 谢谢!我正在拔头发。我不确定为什么在 API 23 之后 windowNoTitle 似乎默认为 true,但感谢您的修复!
    • 真的不明白他们为什么在 API23 中改变了这个
    • 使用parent="@style/Theme.AppCompat.Dialog" 进行暗对话。
    • 我的基本主题扩展了 Theme.AppCompat.DayNight.NoActionBar。如果我覆盖这个,然后是背景,控制颜色的所有变化。您能提出其他解决方案吗?
    【解决方案2】:

    按照你的风格:

    <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);
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        相关资源
        最近更新 更多