使用 XML 在警报对话框中设置消息文本外观(Android API 级别 23)
Setting message text appearance in Alert Dialog using XML (Android API Level 23)
我试图在警告对话框中设置消息的文本大小,但在多次尝试后都无法做到。另一方面,标题和按钮的文本大小可以同时由主题的字体大小控制(在下面的示例中设置为 14sp)。
警报对话框是使用以下 XML 样式描述创建的,遵循 this blog:
中描述的方法
<style name="MyAlertDialogTheme" parent="@android:style/Theme.Holo.Dialog.NoActionBar">
<item name="colorAccent">#ffffff</item>
<!--This controls size of button & title text-->
<item name="android:textSize">14sp</item>
<item name="android:textAppearanceMedium">@style/MyMsgTextAppearance</item>
</style>
<style name="MyMsgTextAppearance" parent="@android:style/TextAppearance.Holo.Medium">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">italic</item>
</style>
主题是在运行时使用以下代码调用的:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogTheme);
builder.setCancelable(false);
builder.setTitle("Remove contact");
builder.setMessage("Are you sure you want to delete this contact ?");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
这是我得到的:
显然,消息文本大小比 22sp 小得多。但是,我可以在运行时调整字体大小:
dialog.show();
TextView tv = (TextView) dialog.findViewById(android.R.id.message);
if(tv != null)
tv.setTextSize(22.0f);
更新
事实证明,如果我导入android.app.AlertDialog,可以使用此方法调整消息大小。但是,如果我导入android.support.v7.app.AlertDialog,则无法使用此方法进行调整。
appcompat-v7 AlertDialog
消息 TextView
具有硬编码样式 @style/TextAppearance.AppCompat.Subhead
而不是 ?android:textAppearanceMedium
。您无法通过覆盖主题属性来更改其文本外观。
但是您可以自定义 appcompat-v7 使用的布局 AlertDialog
。
res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat">
<item name="alertDialogStyle">@style/AlertDialog.Custom</item>
<item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AlertDialog.Custom" parent="AlertDialog.AppCompat">
<item name="android:layout">@layout/alert_dialog_custom</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
...
</style>
res/layout/alert_dialog_custom.xml
复制 abc_alert_dialog_material.xml
(在 Android Studio 中通过双班查找),找到 TextView
和 @android:id/message
并随意更改。
您可以在样式中覆盖 @style/TextAppearance.AppCompat.Subhead
样式,例如:
<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead">
<item name="android:textColor">#000000</item>
<item name="android:textSize">100sp</item>
</style>
并且将使用此样式代替 appcompat 样式
我试图在警告对话框中设置消息的文本大小,但在多次尝试后都无法做到。另一方面,标题和按钮的文本大小可以同时由主题的字体大小控制(在下面的示例中设置为 14sp)。
警报对话框是使用以下 XML 样式描述创建的,遵循 this blog:
中描述的方法<style name="MyAlertDialogTheme" parent="@android:style/Theme.Holo.Dialog.NoActionBar">
<item name="colorAccent">#ffffff</item>
<!--This controls size of button & title text-->
<item name="android:textSize">14sp</item>
<item name="android:textAppearanceMedium">@style/MyMsgTextAppearance</item>
</style>
<style name="MyMsgTextAppearance" parent="@android:style/TextAppearance.Holo.Medium">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">italic</item>
</style>
主题是在运行时使用以下代码调用的:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogTheme);
builder.setCancelable(false);
builder.setTitle("Remove contact");
builder.setMessage("Are you sure you want to delete this contact ?");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
这是我得到的:
显然,消息文本大小比 22sp 小得多。但是,我可以在运行时调整字体大小:
dialog.show();
TextView tv = (TextView) dialog.findViewById(android.R.id.message);
if(tv != null)
tv.setTextSize(22.0f);
更新 事实证明,如果我导入android.app.AlertDialog,可以使用此方法调整消息大小。但是,如果我导入android.support.v7.app.AlertDialog,则无法使用此方法进行调整。
appcompat-v7 AlertDialog
消息 TextView
具有硬编码样式 @style/TextAppearance.AppCompat.Subhead
而不是 ?android:textAppearanceMedium
。您无法通过覆盖主题属性来更改其文本外观。
但是您可以自定义 appcompat-v7 使用的布局 AlertDialog
。
res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat">
<item name="alertDialogStyle">@style/AlertDialog.Custom</item>
<item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AlertDialog.Custom" parent="AlertDialog.AppCompat">
<item name="android:layout">@layout/alert_dialog_custom</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
...
</style>
res/layout/alert_dialog_custom.xml
复制 abc_alert_dialog_material.xml
(在 Android Studio 中通过双班查找),找到 TextView
和 @android:id/message
并随意更改。
您可以在样式中覆盖 @style/TextAppearance.AppCompat.Subhead
样式,例如:
<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead">
<item name="android:textColor">#000000</item>
<item name="android:textSize">100sp</item>
</style>
并且将使用此样式代替 appcompat 样式