为什么我的“警报对话框”无法适应手机的“暗模式”?当我的手机激活了暗模式时,两个按钮的文本几乎看不到,而标题和消息的颜色却反转了。为什么按钮的颜色也不会反转?该图标在非黑暗模式下也不太好显示。如何解决所有这些问题?
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>
答案 0 :(得分:0)
由于您使用的是材料组件主题,因此只需使用<button id="btn">Click to add text to input below</button>
<br /><br />
<input id="input" />
而不是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)