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 - Custom style
<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>
<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);
5 回答
创建
AlertDialog
时,您可以设置要使用的主题 .Example - Creating the Dialog
styles.xml - Custom style
Result
Edit
要更改 Headers 的外观,您可以执行以下操作 . 首先添加一种新风格:
之后只需在_657744中引用此样式:
这样,您可以通过
android:textColorPrimary
为消息定义不同的textColor
,并通过样式为 Headers 定义不同的textColor
.要为所有应用程序使用主题,请不要使用第二个参数来设置Dialog的样式
在我的应用程序中使用主题中的颜色重音不显示带主题colorAccent的alertDialog按钮我必须在主题中添加对话框样式 .
如果你想使用新的android.support.v7.app.AlertDialog并为按钮设置不同的颜色,并且还有自定义布局,那么看看我的https://gist.github.com/JoachimR/6bfbc175d5c8116d411e
关注@reVerse的答案,但在我的情况下,我已经在我的
AppTheme
中有了一些属性所以我的对话框看起来像
我解决了
1)将导入从
android.app.AlertDialog
更改为android.support.v7.app.AlertDialog
2)我在
AppTheme
中使用null值覆盖2属性.
希望它能帮到另一个人