MaterialComponents AlertDialog文本颜色 [英] MaterialComponents AlertDialog text color

本文介绍了MaterialComponents AlertDialog文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 MaterialComponents主题警报对话框按钮

Reading MaterialComponents theme alert dialog buttons and https://medium.com/@lcdsmao/material-design-custom-alert-dialog-5a9cab3ade11 I set AlertDialog buttons and text colors of new Material Theme.

<!-- 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()
}

我知道

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

As you see, buttons colors have changed, but text color and style haven't changed.

然后我尝试了 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 {

得到了

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

How to do the same without adding AlertDialog theme, only redefining Material theme?

推荐答案

使用Material Components,您可以使用以下样式:

With the Material Components you can use a style like:

<!-- 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>

然后您可以在构造函数中引用此样式,例如:

Then you can refer this style in the constructor like:

    new MaterialAlertDialogBuilder(context,
        R.style.MyThemeOverlay.MaterialAlertDialog)

,或者您可以在应用主题中将其设置为默认值:

or you can set it as default in your app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

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

</style>

这篇关于MaterialComponents AlertDialog文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆