我试图在警告对话框中设置消息的文本大小,但在多次尝试后无法进行。 另一方面,标题和按钮文本大小可以同时由主题的字体大小控制(在下面的示例中设置为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.
更多推荐
发布评论