gpt4 book ai didi

android - MaterialComponents AlertDialog 文本颜色

转载 作者:行者123 更新时间:2023-12-04 00:56:47 29 4
gpt4 key购买 nike

阅读 MaterialComponents theme alert dialog buttonshttps://medium.com/@lcdsmao/material-design-custom-alert-dialog-5a9cab3ade11我设置AlertDialog新 Material 主题的按钮和文本颜色。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<!-- AlertDialog -->
<item name="materialAlertDialogBodyTextStyle">@style/MaterialAlertDialogTextTheme</item>
<item name="materialAlertDialogTheme">@style/MaterialAlertDialogButtonsTheme</item>
</style>

<!-- AlertDialog text -->
<style name="MaterialAlertDialogTextTheme" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:colorAccent">@color/colorPrimary</item>
<item name="colorAccent">@color/colorPrimaryitem>
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
</style>

<!-- AlertDialog buttons -->
<style name="MaterialAlertDialogButtonsTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">@style/AlertDialog.Button</item>
...

创建 AlertDialogFragment 后使用
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreateDialog(savedInstanceState)

return MaterialAlertDialogBuilder(context!!).apply {
...
}.create()
}

我明白了

enter image description here

如您所见,按钮颜色已更改,但文本颜色和样式未更改。

然后我尝试了 https://stackoverflow.com/a/51936236/2914140 :
<style name="AlertDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:textColorPrimary">#005B82</item>
<item name="colorAccent">#1b5e20</item>
</style>

...
return MaterialAlertDialogBuilder(context!!, R.style.AlertDialog).apply {

并得到

enter image description here

如何在不添加 AlertDialog 的情况下执行相同操作主题,只重新定义 Material 主题?

最佳答案

使用 Material 组件,您可以使用如下样式:

<!-- Alert Dialog -->
<style name="MyThemeOverlay.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">

<!-- Title -->
<item name="materialAlertDialogTitleTextStyle">@style/MyTitle_MaterialAlertDialog.MaterialComponents.Title.Text</item>


<!-- Body -->
<item name="materialAlertDialogBodyTextStyle">@style/BodyTextAppearance.MaterialComponents.Body2</item>

<!-- Buttons -->
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarNeutralButtonStyle">....</item>
</style>


<style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">#00f</item>
</style>

<style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/primaryDarkColor</item>
</style>

<style name="MyTitle_MaterialAlertDialog.MaterialComponents.Title.Text" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textColor">@color/colorAccent</item>
<item name="android:textAppearance">@style/MyTitle_TextAppearance.MaterialComponents.Subtitle1</item>
</style>

<style name="BodyTextAppearance.MaterialComponents.Body2" parent="@style/TextAppearance.MaterialComponents.Body2">
<item name="android:textColor">@color/colorAccent</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">true</item>
<item name="fontFamily">sans-serif-condensed-light</item>
</style>

enter image description here

然后您可以在构造函数中引用此样式,例如:
    new MaterialAlertDialogBuilder(context,
R.style.MyThemeOverlay.MaterialAlertDialog)

或者您可以在您的应用主题中将其设置为默认值:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

<item name="materialAlertDialogTheme">@style/MyThemeOverlay.MaterialAlertDialog
</item>

</style>

关于android - MaterialComponents AlertDialog 文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61818684/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com