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

时间:2015-05-18 08:25:14

标签: android android-alertdialog android-theme android-styles

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

AlertDialog的定义:

  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>

1 个答案:

答案 0 :(得分:0)

您可以在不使用自定义样式的情况下执行此操作,也可以像下面的代码一样对自定义视图进行充气。

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