自定义AlertDialog样式Theme.AppCompat.Light.Dialog.Alert

时间:2022-02-06 18:55:31

How to make custom style for example for green buttons and background of message using AlertDialog. ???

如何使用AlertDialog为绿色按钮和消息背景制作自定义样式。 ???

Definition of AlertDialog:

AlertDialog的定义:

  AlertDialog.Builder alert = new AlertDialog.Builder(About.this, R.style.MY_AlertDialog);

And style:

<style name="MY.AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:windowBackground">@color/green</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>

</style>

1 个解决方案

#1


0  

You can do it without using custom style or inflating the custom view like below code.

您可以不使用自定义样式或像下面的代码那样对自定义视图进行充气。

public void createDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to exit from app");
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(ViewPagerStyle1Activity.this,
                    "You exit from app", Toast.LENGTH_LONG).show();

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
    TextView messageText = (TextView) alert
            .findViewById(android.R.id.message);
    messageText.setBackgroundColor(Color.RED);
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(Color.MAGENTA);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(Color.YELLOW);
}

#1


0  

You can do it without using custom style or inflating the custom view like below code.

您可以不使用自定义样式或像下面的代码那样对自定义视图进行充气。

public void createDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to exit from app");
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(ViewPagerStyle1Activity.this,
                    "You exit from app", Toast.LENGTH_LONG).show();

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
    TextView messageText = (TextView) alert
            .findViewById(android.R.id.message);
    messageText.setBackgroundColor(Color.RED);
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(Color.MAGENTA);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(Color.YELLOW);
}