如何使用和样式从 appCompat 22.1及以上新的 AlertDialog

我正在尝试从默认的 android AlertDialog迁移到 appCompat-22.1中包含的新版本 到目前为止,我理解您只需要导入 android.support.v7.app.AlertDialog包就可以使用它。

但是我该如何设计它呢? 例如,改变正/负按钮颜色,标题颜色,邮件颜色和背景颜色?

146385 次浏览

在创建 AlertDialog时,可以设置要使用的主题。

示例-创建对话框

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

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>

结果

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

在我的应用程序使用主题颜色口音不显示警报对话框的按钮与主题颜色口音我必须添加一个对话样式的主题。

如果您想使用新的 android.support. v7.app。AlertDialog,并有不同的颜色的按钮,也有一个自定义布局,然后看看我的 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;
}

enter image description here

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

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

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

这样我的对话就像
enter image description here

我用

1)将汇入资料由 android.app.AlertDialog改为 android.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);

希望它能帮助其他人

enter image description here

如果你像我一样,你只是想修改 AppCompat 中的一些颜色,你唯一需要在对话框中唯一改变的颜色就是背景。然后你需要做的就是为 colorBackgroundFloating设置一个颜色。

下面是我的基本主题,它只是修改一些没有嵌套主题的颜色:

    <style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">@color/theme_colorPrimary</item>
<item name="colorPrimaryDark">@color/theme_colorPrimaryDark</item>
<item name="colorAccent">@color/theme_colorAccent</item>
<item name="colorControlActivated">@color/theme_colorControlActivated</item>
<item name="android:windowBackground">@color/theme_bg</item>
<item name="colorBackgroundFloating">@color/theme_dialog_bg</item><!-- Dialog background color -->
<item name="colorButtonNormal">@color/theme_colorPrimary</item>
<item name="colorControlHighlight">@color/theme_colorAccent</item>
</style>