Tom*_*ask 5 android android-theme android-alertdialog android-styles
我正在尝试通过样式更改警报对话框中消息的字体大小,但无法使其正常工作。有很多答案描述了如何执行此操作,但是我发现它们几乎都是以编程方式执行的。我想只使用一次样式,而不是使用每个新的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" />
Run Code Online (Sandbox Code Playgroud)
在\AndroidSDK\platforms\android-23\data\res\values\themes.xml我看到的textAppearanceMedium属性值是@style/TextAppearance.Medium。
我创建了以下父样式为的样式TextAppearance.AppCompat.Medium。然后将该样式应用于android:textAppearanceMediumAlertDialog样式的属性,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
...但是字体大小不会改变。怎么了?
我正在使用AppCompat支持库,最低SDK 16,目标SDK 23。
谢谢。
?android:attr/textAppearanceMedium"在 Material Theme 之前,AlertDialog 一直使用主题属性作为对话框内容。
如果您查看AlertDialog它的源代码,它使用 anAlertController来管理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);
Run Code Online (Sandbox Code Playgroud)
我们可以看到, 的布局AlertDialog来自一个名为“布局”declare-styleable name=AlertDialog的属性,该属性在属性中声明和定义在主题级别alertDialogStyle。
知道,alertDialogStyle在 Material 主题中我们得到了这个
<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" />
Run Code Online (Sandbox Code Playgroud)
但是样式指向本地主题而不是,?android:attr/textAppearanceMedium这就是为什么您不能像以前一样设置消息样式。
解决方案1:
我们只能以编程方式执行此操作。TextView从对话框中获取使用窗口并设置您想要的外观:
android.support.v7.app.AlertDialog d = builder.show();
TextView tv = (TextView)d.getWindow().findViewById(android.R.id.message);
Run Code Online (Sandbox Code Playgroud)
解决方案2:
为了使它更灵活,我们可以在 attrs.xml
<resources>
<attr name="dialogMessageStyle" format="reference"/>
</resources>
Run Code Online (Sandbox Code Playgroud)
在您的主题中定义属性:
<style name="MyStyle" ...>
<item name="dialogMessageStyle">@style/AlertDialogMessageAppearance</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并使用以下代码在代码中设置它:
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));
Run Code Online (Sandbox Code Playgroud)
如果我们查看 android 源代码,我们会发现Theme.AppCompat.Light.Dialog.Alert主题继承了 alertDialogStyle:
<item name="alertDialogStyle">@style/AlertDialog.AppCompat.Light</item>
Run Code Online (Sandbox Code Playgroud)
通过AlertDialog.AppCompat.Light样式我们会发现它使用默认布局:
<item name="android:layout">@layout/abc_alert_dialog_material</item>
Run Code Online (Sandbox Code Playgroud)
进一步调查来源,特别是 layout @layout/abc_alert_dialog_material,我们会看到TextViewwith id android:id="@android:id/message"(this is message TextView) 使用 styleTextAppearance.AppCompat.Subhead
<TextView
android:id="@android:id/message"
style="@style/TextAppearance.AppCompat.Subhead"
... />
Run Code Online (Sandbox Code Playgroud)
覆盖应用程序values/styles.xml文件中的这些样式将更改警报对话框中消息的外观:
<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#ff0000</item> <!-- we can change other parameters, e.g. textColor -->
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3753 次 |
| 最近记录: |