如何使用 appCompat 22.1 及更高版本的新 AlertDialog 并设置其样式
How to use and style new AlertDialog from appCompat 22.1 and above
我正在尝试从默认 android AlertDialog
迁移到 appCompat-22.1 中包含的新版本
到目前为止,我了解到您只需导入 android.support.v7.app.AlertDialog
包即可使用它。
但我该如何设置样式呢?例如更改 positive/negative 按钮颜色、标题颜色、消息颜色和背景颜色?
创建 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();
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>
结果
编辑
要更改标题的外观,您可以执行以下操作。先添加新样式:
<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
,通过样式为标题定义不同的
。
为所有应用程序使用一个主题,不要使用第二个参数来设置对话框的样式
<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>
在我的应用程序中,在主题中使用颜色强调不显示带有主题 colorAccent 的 alertDialog 按钮,我必须在主题中添加对话框样式。
如果您想使用新的 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;
}
<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>
所以我的对话框看起来像
我通过
解决了
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);
希望对其他人有所帮助
如果您像我一样只想修改 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>
我正在尝试从默认 android AlertDialog
迁移到 appCompat-22.1 中包含的新版本
到目前为止,我了解到您只需导入 android.support.v7.app.AlertDialog
包即可使用它。
但我该如何设置样式呢?例如更改 positive/negative 按钮颜色、标题颜色、消息颜色和背景颜色?
创建 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();
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>
结果
编辑
要更改标题的外观,您可以执行以下操作。先添加新样式:
<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
,通过样式为标题定义不同的
为所有应用程序使用一个主题,不要使用第二个参数来设置对话框的样式
<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>
在我的应用程序中,在主题中使用颜色强调不显示带有主题 colorAccent 的 alertDialog 按钮,我必须在主题中添加对话框样式。
如果您想使用新的 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;
}
<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>
所以我的对话框看起来像
我通过
解决了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);
希望对其他人有所帮助
如果您像我一样只想修改 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>