如何从appCompat 22.1及更高版本使用和设置新的AlertDialog

我想从默认的android AlertDialog迁移到appCompat-22.1中包含的新的到目前为止,我明白你只需要导入android.support.v7.app.AlertDialog包来使用它。

但是我怎么样呢? 例如更改正面/负面的button颜色,标题颜色,消息颜色和背景颜色?

创buildAlertDialog您可以设置要使用的主题。

示例 – 创build对话框

 AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle); builder.setTitle("AppCompatDialog"); builder.setMessage("Lorem ipsum dolor..."); builder.setPositiveButton("OK", null); builder.setNegativeButton("Cancel", null); builder.show(); 

styles.xml – 自定义样式

 <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Used for the buttons --> <item name="colorAccent">#FFC107</item> <!-- Used for the title and text --> <item name="android:textColorPrimary">#FFFFFF</item> <!-- Used for the background --> <item name="android:background">#4CAF50</item> </style> 

结果

风格alertdialog

编辑

为了更改标题的外观,您可以执行以下操作。 首先添加一个新的风格:

 <style name="MyTitleTextStyle"> <item name="android:textColor">#FFEB3B</item> <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item> </style> 

之后只需在MyAlertDialogStyle引用此样式:

 <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> ... <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item> </style> 

这样,你可以通过android:textColorPrimary textColor为消息定义一个不同的textColor ,并通过style定义一个不同的标题。

要为所有应用程序使用主题,并且不要使用第二个参数来设置对话框的样式

 <style name="MyTheme" parent="Base.Theme.AppCompat.Light"> <item name="alertDialogTheme">@style/dialog</item> <item name="colorAccent">@color/accent</item> </style> <style name="dialog" parent="Base.Theme.AppCompat.Light.Dialog.Alert"> <item name="colorAccent">@color/accent</item> </style> 

在我的应用程序在主题中使用颜色重音不显示alertDialog的button与主题colorAccent我必须在主题中添加对话框样式。

如果你想使用新的android.support.v7.app.AlertDialog,并有不同的button颜色,也有一个自定义的布局然后看看我的https://gist.github.com/JoachimR/6bfbc175d5c8116d411e

 @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { View v = inflater.inflate(R.layout.custom_layout, null); initDialogUi(v); final AlertDialog d = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle) .setTitle(getString(R.string.some_dialog_title)) .setCancelable(true) .setPositiveButton(activity.getString(R.string.some_dialog_title_btn_positive), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { doSomething(); dismiss(); } }) .setNegativeButton(activity.getString(R.string.some_dialog_title_btn_negative), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dismiss(); } }) .setView(v) .create(); // change color of positive button d.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button b = d.getButton(DialogInterface.BUTTON_POSITIVE); b.setTextColor(getResources().getColor(R.color.colorPrimary)); } }); return d; } 

在这里输入图像描述

按照@reVerse的答案,但在我的情况下,我已经在我的AppTheme有一些属性

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> ... <item name="android:textColor">#111</item> <item name="android:textSize">13sp</item> </style> 

所以我的对话框看起来像
在这里输入图像描述

我解决了它

1)更改从android.app.AlertDialogandroid.support.v7.app.AlertDialog的导入
2)我重写AppTheme 2个属性与空值

 <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Used for the buttons --> <item name="colorAccent">#FFC107</item> <!-- Used for the title and text --> <item name="android:textColorPrimary">#FFFFFF</item> <!-- Used for the background --> <item name="android:background">#4CAF50</item> <item name="android:textColor">@null</item> <item name="android:textSize">@null</item> </style> 

 AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle); 

希望能帮助别人

在这里输入图像描述

  <item name="editTextColor">@color/white</item> <item name="android:textColor">@color/white</item> <item name="android:textColorHint">@color/gray</item> <item name="android:textColorPrimary">@color/gray</item> <item name="colorControlNormal">@color/gray</item> <item name="colorControlActivated">@color/white</item> <item name="colorControlHighlight">#30FFFFFF</item>