Android 6.0对话框文本不会出现

Vek*_*eka 27 android android-alertdialog android-dialog android-dialogfragment android-6.0-marshmallow

我将手机更新到Android 6.0,我有两个对话框问题:

1)显示标题,但消息不用于警告对话框(已解决):

        new AlertDialog.Builder(context).setTitle("Title").setMessage("Message");
Run Code Online (Sandbox Code Playgroud)

2)也没有显示自定义对话框片段的标题(未解决):

        getDialog().setTitle("Title");
Run Code Online (Sandbox Code Playgroud)

在棒棒糖或旧版本中没有这样的问题,只有在将手机更新为棉花糖后才出现问题.

如何解决问题?

Ole*_*ndr 39

使用带有主题的构造函数用于Lollipop和更新的Android版本:

黑暗的主题

    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(context);
    }
Run Code Online (Sandbox Code Playgroud)

而对于Light主题

    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(context);
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您有很多对话框,这不是一个好方法.@Stephen答案是最好的. (2认同)

Ste*_*hen 30

我遇到了一些答案,说你应该在你的应用主题中使用以下内容:

<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
Run Code Online (Sandbox Code Playgroud)

虽然这是正确的,但它并不适用于所有地方.最后,我意识到在某些地方我使用常规的AlertDialog构建器,而在其他地方我使用的是支持构建器.如果您使用支持AlertDialog,请确保在主题中包含以下内容:

<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
Run Code Online (Sandbox Code Playgroud)

  • 这个答案应该被推到顶端. (4认同)

Yas*_*dia 18

只需将它放在您的基本主题样式标记内的样式文件中,就可以了

<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
Run Code Online (Sandbox Code Playgroud)


小智 9

答案2),我们需要setStyle()之前打电话,onCreateDialog()因为theme用于onCreateDialog()

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog_Alert);
    }
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setTitle(R.string.dialog_title);
    return dialog;
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*ihi 7

我尝试通过制作自己的风格来解决这个问题.

你的对话框对象应该是这样的,你的样式指向这个对话框将写在下面

new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))
            .setTitle("Title")
            .setMessage("Sample Message ...").show();
Run Code Online (Sandbox Code Playgroud)

R.style.Dialog ::

<style name="Dialog">
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColor">@color/primary_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

颜色::

<color name="primary_text">#212121</color>
<color name="primary_color">#2196f3</color>
Run Code Online (Sandbox Code Playgroud)

最后输出应该是这样的

注意:"android:textColorPrimary"用于对话框setMessage,"android:textColor"用于对话框setTitle; 我不在对话框对象中使用setPositive和setNegative按钮和侦听器,但如果您希望自己可以创建自己的对话框对象,则可以在图片中看到它们.


Ama*_*ana 1

new AlertDialog.Builder(context)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助.............