【问题标题】:Alert Dialog doesn't adapt to Dark Mode and non-dark mode警报对话框不适应暗模式和非暗模式
【发布时间】:2020-07-17 14:48:10
【问题描述】:

为什么我的提醒对话框不适应我手机的深色模式?当在我的手机中激活暗模式时,两个按钮的文本几乎看不到,而标题和消息的颜色是相反的。为什么按钮颜色也不反转?该图标在非深色模式下也不会很好地显示。如何解决所有这些问题?

  val builder = AlertDialog.Builder(this)
  builder.setTitle(getString(R.string.title))
  builder.setMessage(getString(R.string.message))

  builder.setPositiveButton(getString(R.string.positive)) { dialog, which ->
        
    Toast.makeText(applicationContext, getString(R.string.pos_toast), Toast.LENGTH_LONG).show()
  }

  builder.setNegativeButton(getString(R.string.negative)) { dialog, which ->

    Toast.makeText(applicationContext, getString(R.string.negative_toast), Toast.LENGTH_LONG).show()
  }

  builder.setIcon(android.R.drawable.ic_dialog_alert)
  builder.show()

我的 stiles.xml:

<resources>

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

  <style name="generalnotitle" parent="MyTheme">
    <item name="android:windowNoTitle">true</item>
  </style>

</resources>

【问题讨论】:

  • 您使用的是哪个应用主题?
  • @GabrieleMariotti 请参阅编辑。

标签: android kotlin android-alertdialog android-theme material-components-android


【解决方案1】:

由于您使用的是 Material Components 主题,因此只需使用 MaterialAlertDialogBuilder 而不是 AlertDialog.Builder:

    MaterialAlertDialogBuilder(this)
            .setTitle("Title")
            .setMessage("Message")
            .setNegativeButton("CANCEL") { dialog, which ->
                // Respond to neutral button press
            }
            .setPositiveButton("OK") { dialog, which ->
                // Respond to positive button press
            }
            .show()

按钮的默认文本颜色基于此选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checkable="true" android:state_checked="true" android:state_enabled="true"/>
  <item android:alpha="0.60" android:color="?attr/colorOnSurface" android:state_checkable="true" android:state_checked="false" android:state_enabled="true"/>
  <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_enabled="true"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>

只需检查您的应用主题中定义的 colorPrimary 是否有暗模式。

如果您想更改按钮的文本颜色,可以使用以下命令覆盖 colorPrimary:

MaterialAlertDialogBuilder(this, R.style.Theme_MyApp_Dialog_Alert)

与:

<style name="Theme.MyApp.Dialog.Alert" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">

    <!-- Text Color for Buttons -->
    <item name="colorPrimary">@color/...</item>

</style>

【讨论】:

  • 好吧,我还是接受了你的回答,我把我自己的颜色。但是为什么文本颜色不会像标题和消息一样自行改变?
  • @Panjeet 我已经更新了答案。按钮的文本颜色基于colorPrimary。如果您在values-night 文件夹中定义了colorPrimary,则必须签入您的应用主题。短信和标题基于colorOnSurface。如果您尚未在应用主题中定义它,则有一个默认值。
猜你喜欢
  • 1970-01-01
  • 2020-01-05
  • 2019-11-15
  • 2015-03-08
  • 2019-05-09
  • 2020-07-07
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多