How to apply color in text message in alert dialog

2019-04-01 03:20发布

问题:

I am developing android app. I am using alert dialog box. How can alert message color?

 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
              builder.setMessage("...........congratulations...........");
                   builder.setCancelable(false)
                .setPositiveButton("Move Next Level",
                        new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) 
                                    {


                                    }
                                });

                AlertDialog alert = builder.create();
               alert.setTitle(".");
               alert.setIcon(R.drawable.newmovw);      
                alert.show();   

Display message, text how can apply color in Android.

回答1:

you can create a custom view to the Alert Dialog Content. which is explained in the code on APIDemos itself clearly. check the case DIALOG_TEXT_ENTRY in it. the class behind it is LayoutInflater. Please check that too.

For Example:

LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);// this layout can created by yourself whatever you want.
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();

Hope it helps.

Edit:

layoutname : main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent" android:textColor="#663355"
        android:layout_height="wrap_content" android:hint="@string/hello" />
</LinearLayout>

If the layout name is main.xml then you have to change the code like below:

factory.inflate(R.layout.main, null);