警告对话框不适应黑暗模式和 non-dark 模式
Alert Dialog doesn't adapt to Dark Mode and non-dark mode
为什么我的警报对话框不适应我的 phone 的深色模式?当在我的 phone 中激活深色模式时,两个按钮的文本几乎看不见,而标题和消息的颜色是颠倒的。为什么按钮的颜色也不反转?该图标在 non-dark 模式下也显示不佳。如何解决所有这些问题?
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 组件主题,只需使用 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>
为什么我的警报对话框不适应我的 phone 的深色模式?当在我的 phone 中激活深色模式时,两个按钮的文本几乎看不见,而标题和消息的颜色是颠倒的。为什么按钮的颜色也不反转?该图标在 non-dark 模式下也显示不佳。如何解决所有这些问题?
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 组件主题,只需使用 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>