Android:alertdialog中通过样式显示的消息字体大小 [英] Android: message font size in alertdialog through styles
问题描述
我正在尝试通过样式更改警报对话框中消息的字体大小,但无法使其正常工作.有很多答案描述了如何执行此操作,但是我发现它们几乎都是以编程方式完成的.我想只使用一次样式,而不是使用每个新的Alertdialog.
I'm trying to change font size of message in alert dialog through styles but I can't make it work. There is a lot of answers describing how to do this but almost all of them I found do it programmatically. I want to do this once in styles, not with every new alertdialog.
到目前为止我所做的:
在\AndroidSDK\platforms\android-23\data\res\layout\alert_dialog.xml
中,我看到显示消息的TextView:
In \AndroidSDK\platforms\android-23\data\res\layout\alert_dialog.xml
I see the TextView that is showing the message:
<TextView android:id="@+id/message"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip" />
在\AndroidSDK\platforms\android-23\data\res\values\themes.xml
中,我看到textAppearanceMedium
属性的值是@style/TextAppearance.Medium
.
In \AndroidSDK\platforms\android-23\data\res\values\themes.xml
I see that the value of textAppearanceMedium
attribute is @style/TextAppearance.Medium
.
我创建了以下样式,其父样式为TextAppearance.AppCompat.Medium
.然后将此样式应用于AlertDialog样式的android:textAppearanceMedium
属性,如下所示:
I've created a following style of which the parent is TextAppearance.AppCompat.Medium
. This style is then applied to android:textAppearanceMedium
attribute of AlertDialog's style as follows:
<style name="Theme.My" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
<item name="android:alertDialogTheme">@style/Theme.My.AlertDialog</item>
</style>
<style name="Theme.My.AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textAppearanceMedium">@style/MyTextAppearanceMedium</item>
</style>
<style name="MyTextAppearanceMedium" parent="TextAppearance.AppCompat.Medium">
<item name="android:textSize">24sp</item>
</style>
...但是字体大小不会改变.怎么了?
...but the font size won't change. What's wrong?
我正在使用AppCompat支持库,最小SDK 16,目标SDK 23.
I'm using AppCompat support lib, min SDK 16, target SDK 23.
谢谢.
推荐答案
AlertDialog使用主题属性?android:attr/textAppearanceMedium"
作为对话框内容,直到材料主题"为止.
AlertDialog was using the theme attribute ?android:attr/textAppearanceMedium"
for dialog content until Material Theme.
如果您查看AlertDialog
的源代码,它将使用AlertController
来管理AlertDialog
.在AlertDialog
构造函数中,我们有:
If you look at the source code of AlertDialog
it uses an AlertController
to manage the AlertDialog
. In AlertDialog
constructor we have this :
TypedArray a = context.obtainStyledAttributes(null,
com.android.internal.R.styleable.AlertDialog,
com.android.internal.R.attr.alertDialogStyle, 0);
mAlertDialogLayout = a.getResourceId(com.android.internal.R.styleable.AlertDialog_layout,
com.android.internal.R.layout.alert_dialog);
我们可以看到AlertDialog
的布局来自declare-styleable name=AlertDialog
中声明的属性" layout ",并在主题级别的属性alertDialogStyle
中定义.
We can see that the layout for the AlertDialog
comes from an attribute called "layout" declared in declare-styleable name=AlertDialog
and defined at theme level in the attribute alertDialogStyle
.
知道,在Material主题中查看alertDialogStyle
,我们得到了这个
<item name="alertDialogStyle">@style/AlertDialog.Material</item>
Know, looking at alertDialogStyle
in Material theme we got this
<item name="alertDialogStyle">@style/AlertDialog.Material</item>
,在 Alert.Dialog.Material 处,我们得到了<item name="layout">@layout/alert_dialog_material</item>
它使用的是 alert_dialog_material ,而不是 alert_dialog .
在 alert_dialog_material 中,我们具有相同的TextView
In alert_dialog_material we have the same TextView
<TextView android:id="@+id/message"
style="@style/TextAppearance.Material.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/alert_dialog_padding_material"
android:paddingTop="@dimen/alert_dialog_padding_top_material"
android:paddingEnd="@dimen/alert_dialog_padding_material" />
但是样式指向本地主题而不是?android:attr/textAppearanceMedium
,这就是为什么您不能像以前那样对消息进行样式设置.
but the style points to a local theme instead of ?android:attr/textAppearanceMedium
and that's why you can't style the message like before.
解决方案1:
我们只能以编程方式执行此操作.使用对话框中的窗口获取TextView
并设置所需的外观:
We can only do this programmatically. Get the TextView
using the window from the Dialog and set the appearance you want:
android.support.v7.app.AlertDialog d = builder.show();
TextView tv = (TextView)d.getWindow().findViewById(android.R.id.message);
解决方案2:
为使其更加灵活,我们可以在attrs.xml
To make it more flexible we can define an attribute in attrs.xml
<resources>
<attr name="dialogMessageStyle" format="reference"/>
</resources>
在主题中定义属性:
<style name="MyStyle" ...>
<item name="dialogMessageStyle">@style/AlertDialogMessageAppearance</item>
</style>
并使用以下代码进行设置:
And set it in code using:
TextView tv = (TextView)d.getWindow().findViewById(android.R.id.message);
TypedArray a = getTheme().obtainStyledAttributes(new int[]{R.attr.dialogMessageStyle});
tv.setTextAppearance(DialogActivity.this,a.getResourceId(0,0));
这篇关于Android:alertdialog中通过样式显示的消息字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!