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

Custom AlertDialog style Theme.AppCompat.Light.Dialog.Alert

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

A​​lertDialog 的定义:

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

和风格:

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

您可以在不使用自定义样式或像下面的代码那样膨胀自定义视图的情况下做到这一点。

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