使用XML在警报对话框中设置消息文本外观(Android API Level 23)(Setting message text appearance in Alert Dialog using XML

编程入门 行业动态 更新时间:2024-10-27 03:26:18
使用XML在警报对话框中设置消息文本外观(Android API Level 23)(Setting message text appearance in Alert Dialog using XML (Android API Level 23))

我试图在警告对话框中设置消息的文本大小,但在多次尝试后无法进行。 另一方面,标题和按钮文本大小可以同时由主题的字体大小控制(在下面的示例中设置为14sp)。

使用以下XML样式描述创建警报对话框,遵循此博客中描述的方法:

<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 ,则无法使用此方法进行调整。

I was trying to set the text size of message in an alert dialog, but was unable to after many attempts. On the other hand, the title and button text sizes can be controlled by the theme's font size simultaneously (set to 14sp in the example below).

The alert dialog was created using the following XML style description, following the approach described in 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>

The theme was invoked at runtime using this code:

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

Here is what I got:

Apparently, the message text size is much smaller than 22sp. I could, however, adjust the font size during runtime:

dialog.show(); TextView tv = (TextView) dialog.findViewById(android.R.id.message); if(tv != null) tv.setTextSize(22.0f);

Update It turns out that if I import android.app.AlertDialog, the message size can be adjusted using this method. However, if I import android.support.v7.app.AlertDialog, it can't be adjusted using this method.

最满意答案

appcompat-v7 AlertDialog消息TextView具有硬编码样式@style/TextAppearance.AppCompat.Subhead而不是?android:textAppearanceMedium 。 您无法通过覆盖主题属性来更改其文本外观。

但是,您可以自定义appcompat-v7 AlertDialog使用的布局。

RES /值/ 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 /布局/ alert_dialog_custom.xml

复制abc_alert_dialog_material.xml (在Android Studio中通过双班查找),找到带有@android:id/message的TextView并根据需要进行更改。

appcompat-v7 AlertDialog message TextView has hardcoded style @style/TextAppearance.AppCompat.Subhead instead of ?android:textAppearanceMedium. You cannot change its text appearance by overriding the theme attribute.

However you can customize the layout used by 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

Make a copy of abc_alert_dialog_material.xml (find via double-shift in Android Studio), find a TextView with @android:id/message and alter it as you please.

更多推荐

本文发布于:2023-08-04 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   外观   文本   消息   对话框中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!