如何使用和appCompat 22.1样式的新AlertDialog [英] How to Use and Style the new AlertDialog from appCompat 22.1
问题描述
我想从默认的Android AlertDialog迁移到新列入appCompat 22.1
到目前为止,我理解你只需要导入 android.support.v7.app.AlertDialog;
为了使用它
I am trying to migrate from default android AlertDialog to the new one included in appCompat 22.1
So Far I understand you only have to import the android.support.v7.app.AlertDialog;
in order to use it.
不过,我怎么可以风格呢?例如改变正/负键的颜色,标题颜色,消息颜色和背景颜色?
But How can I style it? For example change the positive/negative button colors, Title color, message color and background color?
推荐答案
创建当 AlertDialog
你可以设置一个主题来使用。
When creating the AlertDialog
you can set a theme to use.
示例 - 创建对话框
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>
结果
修改
为了改变标题的外观,可以做到以下几点。首先添加一个新的风格:
In order to change the Appearance of the Title, you can do the following. First add a new 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>
此方式,您可以定义不同的文字颜色
通过邮件机器人:textColorPrimary
和标题不同通过风格。
This way you can define a different textColor
for the message via android:textColorPrimary
and a different for the title via the style.
这篇关于如何使用和appCompat 22.1样式的新AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!