Android 6.0 Dialog text doesn't appear

2019-01-23 11:51发布

I updated my phone to Android 6.0 and I have these 2 problems with dialogs:

1)The title is shown but the messages isn't for alert dialog(SOLVED):

        new AlertDialog.Builder(context).setTitle("Title").setMessage("Message");

2)Also custom dialog fragment's title is not shown(NOT SOLVED):

        getDialog().setTitle("Title");

There was not such a problem in lollipop or in older versions, the problem appeared only after updating my phone to marshmallow.

How to solve the problem?

11条回答
Deceive 欺骗
2楼-- · 2019-01-23 12:13
@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }

Once check this hope this will work.......

查看更多
女痞
3楼-- · 2019-01-23 12:14

Maybe your main text color is the same as dialog background color, in that case in your styles.xml use (just an example):

<style name="AlertDialog" parent="@android:style/Theme.Material.Light.Dialog.Alert">
    <item name="android:textColor">#000000</item>
</style>

and in dialog creation:

new AlertDialog.Builder(
        new ContextThemeWrapper(getContext(), R.style.AlertDialog))
).setMessage("test");
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-23 12:16

Use constructor with theme for Lollipop and newer android versions:

Dark theme

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

And for Light theme

    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);
    }
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-23 12:16

I've come across a few answers that say that you should use the following in your app theme:

<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>

While that is correct, it wasn't working in all places for me. Eventually, I realized that in some places I was using the regular AlertDialog builder and in other places I was using the support builder. If you are using the support AlertDialog, make sure to also include the following in your theme:

<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
查看更多
混吃等死
6楼-- · 2019-01-23 12:17

In my case all dialogs had the same color as fonts (title & message). What I have done to fix this state was the change of the color attribute in my theme. I copied the xml theme file into the 'res/values-v22' and set different color for marshmallow.

<resources>
    <style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:textColorPrimary">@android:color/tertiary_text_dark</item>
    </style>
</resources>

or only for dialogs

<resources>
    <style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:alertDialogTheme">@style/LywAlertDialogStyle</item>
    </style>
    <style name="LywAlertDialogStyle" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColorPrimary">@color/lyw_secondary_text_color</item>
    </style>
</resources>
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-23 12:18

I suspect that you are ending up showing white text on a white background! (Looking at the screenshot, the (i) icon is not showing up well either, suggesting that it was designed to be shown on a background other than white.

You can use the constructor public AlertDialog.Builder (Context context, int themeResId) to ensure you are using a specific theme to style your dialog, where the Theme_Material_Dialog is probably what you want.

查看更多
登录 后发表回答