问题描述
我决定尝试在Android上尝试对话框.我遇到的问题是尝试应用某些样式时.检查文档,我发现了这一点:
I decided to try material alert dialogs on android.The problem I am having is when I try to apply some styles. Checking the docs, I found this:
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
所以我决定尝试一下:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="materialAlertDialogTheme">@style/Theme.App.MaterialDialogAlert</item>
</style>
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<!-- FIXME: this does not work. it does not change the title appearance. -->
<!-- <item name="materialAlertDialogTitleTextStyle">@style/TextAppearance.App.MaterialDialogAlert.Title.Text</item>-->
<!-- FIXME: this change only the title font, not the message appearance -->
<item name="android:fontFamily">@font/nunito_semi_bold</item>
<item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button.TextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="TextAppearance.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
但是到目前为止,我可以成功设置按钮的样式.关于标题和消息,我有一些问题.现在,我可以通过以下方式将某些样式应用于标题:
But so far I could style the buttons successfully. With the title and message I have some issues. Now I am able to apply some styles to the title with this:
<item name="android:fontFamily">@font/nunito_semi_bold</item>
但是我认为这不是正确的方法.首先,这不会将字体应用于消息部分.其次,我想应用其他文本样式,一种用于标题,一种用于消息部分.
But I do not think this is the proper way. First this does not apply the font to the message section. Second, I would like to apply other text styles, one for title and one for the message section.
我也尝试过检查以下样式:MaterialAlertDialog.MaterialComponents.Title.Text
在某处可以应用该项目:
I tried also checking this style: MaterialAlertDialog.MaterialComponents.Title.Text
where I can see that this item is being applied at some point:
<item name="android:textAppearance">?attr/textAppearanceSubtitle1</item>
所以我决定在基本主题中声明这一点:
So I decided to declare this in my base theme:
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="textAppearanceSubtitle1">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
但没有任何改变.
所以我的问题是:
- 如何使用主题/样式正确执行此操作.
- 我当前的代码在做什么错了?
编辑我也尝试过使用它:
EDITI tried using this too:
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="materialAlertDialogTitleTextStyle">@style/Dialog.App.MaterialDialogAlert.Title.Text</item>
<item name="materialAlertDialogBodyTextStyle">@style/TextAppearance.App.MaterialDialogAlert.Body.Text</item>
<item name="buttonBarPositiveButtonStyle">@style/Widget.App.Button.TextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="Dialog.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">@font/nunito_semi_bold</item>
<item name="android:fontFamily">@font/nunito_semi_bold</item>
</style>
但是它不起作用.
我也从材料组件回购中尝试过
Also I tried this from the material components repo:
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="materialAlertDialogTitleTextStyle">@style/Dialog.App.MaterialDialogAlert.Title.Text</item>
<item name="materialAlertDialogBodyTextStyle">@style/Dialog.App.MaterialDialogAlert.Body.Text</item>
<item name="buttonBarButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>
<style name="Dialog.App.MaterialDialogAlert.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/TextAppearance.App.Subtitle1</item>
</style>
<style name="Dialog.App.MaterialDialogAlert.Body.Text" parent="@style/MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textAppearance">@style/TextAppearance.Body2</item>
<item name="fontFamily">@font/nunito_regular</item>
<item name="android:fontFamily">@font/nunito_regular</item>
<item name="android:textColor">@color/color_error</item>
</style>
但不起作用.
我唯一可以自定义的想法就是将标题添加到主题中:
The only think I could customise was the title by adding this to the theme:
<item name="android:windowTitleStyle">@style/Dialog.App.MaterialDialogAlert.Title.Text</item>
我正在检查库中的代码,并且可以看到正在使用此布局: mtrl_alert_dialog.xml
在这里我可以看到:
<TextView
android:id="@android:id/message"
style="?attr/materialAlertDialogBodyTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"/>
在这里我们可以看到对materialAlertDialogBodyTextStyle的引用.也许这是库中的错误?
where we can see a reference to materialAlertDialogBodyTextStyle. So maybe it is a bug in the library?
推荐答案
您可以使用以下样式来自定义标题和正文:
You can use this style to customize the title and the body:
<style name="Theme.App.MaterialDialogAlert" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<!-- Title -->
<item name="materialAlertDialogTitleTextStyle">@style/MyTitle_MaterialAlertDialog</item>
<!-- Body -->
<item name="materialAlertDialogBodyTextStyle">@style/BodyMaterialAlertDialog.MaterialComponents.Body.Text</item>
</style>
然后定义标题的样式:
<style name="MyTitle_MaterialAlertDialog" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textAppearance">@style/MyTitle_TextAppearance.MaterialComponents.Subtitle1</item>
</style>
<style name="MyTitle_TextAppearance.MaterialComponents.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">.....</item>
<item name="android:fontFamily">....</item>
<item name="android:textStyle">.....</item>
</style>
然后为主体定义:
<style name="BodyTextAppearance.MaterialComponents.Body2" parent="@style/TextAppearance.MaterialComponents.Body2">
<item name="android:textColor">@color/....</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">true</item>
<item name="fontFamily">.....</item>
<item name="android:fontFamily">....</item>
</style>
这篇关于将样式应用于警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!