android - 警报对话框不适应暗模式和非暗模式

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

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

  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>

最佳答案

由于您使用的是 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>
enter image description here
只需检查 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>
enter image description here

关于android - 警报对话框不适应暗模式和非暗模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62956432/

相关文章:

android - 如何通过 jsoup 从以下 scipt 获取图像

kotlin - 如何使用 spring-cloud-function 消费 Kafka 事件,但有时只产生

android - kotlin 中的 MPAndroid 饼图

android - 如何处理警报对话框中的无效输入?

android - 从服务启动时对话框 Activity 打开两次

android - 使部分 TextView 可点击(不是 url)

java - 即使引用检索数据,Firebase 查询也无法在 Android 上运行

java - Android Studio : Error:Execution failed for task ':app:transformClassesWithJarMergingForRelease' .>

android - Anko布局DSL : How to use already existing layout?

android - 如何在 Android 中测试 AlertDialog?