gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:52:35 26 4
gpt4 key购买 nike

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

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