我正在尝试通过样式更改警报对话框中消息的字体大小,但无法使其正常工作。有很多答案描述了如何执行此操作,但是我发现它们几乎都是以编程方式完成的。我想只使用一次样式,而不要使用每个新的alertdialog。
到目前为止,我所做的是:
在\AndroidSDK\platforms\android-23\data\res\layout\alert_dialog.xml
中,我看到了显示消息的TextView:
<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
。我创建了以下样式,其父样式为
TextAppearance.AppCompat.Medium
。然后将此样式应用于AlertDialog样式的android:textAppearanceMedium
属性,如下所示:<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>
...但是字体大小不会改变。怎么了?
我正在使用AppCompat支持库,最小SDK 16,目标SDK 23。
谢谢你。
最佳答案
AlertDialog一直使用主题属性?android:attr/textAppearanceMedium"
作为对话框内容,直到Material Theme。
如果您查看AlertDialog
的源代码,它将使用AlertController
来管理AlertDialog
。在AlertDialog
构造函数中,我们有:
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
中声明并在主题级别alertDialogStyle
中定义的名为“ layout ”的属性。知道了,在Material主题中查看
alertDialogStyle
,我们得到了这个<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
<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
,这就是为什么您无法像以前那样对消息进行样式设置的原因。解决方案1:
我们只能以编程方式执行此操作。使用对话框中的窗口获取
TextView
并设置所需的外观:android.support.v7.app.AlertDialog d = builder.show();
TextView tv = (TextView)d.getWindow().findViewById(android.R.id.message);
解决方案2:
为了使其更加灵活,我们可以在
attrs.xml
中定义一个属性<resources>
<attr name="dialogMessageStyle" format="reference"/>
</resources>
在主题中定义属性:
<style name="MyStyle" ...>
<item name="dialogMessageStyle">@style/AlertDialogMessageAppearance</item>
</style>
并使用以下代码在代码中进行设置:
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));