Pan*_*eet 1 android android-theme android-alertdialog kotlin material-components-android
为什么我的警报对话框不适应我手机的暗模式?在我的手机中激活暗模式时,几乎看不到两个按钮的文本,而标题和消息颜色则相反。为什么按钮颜色也不反转?该图标在非深色模式下也不能很好地显示。如何解决所有这些问题?
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()
Run Code Online (Sandbox Code Playgroud)
我的 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>
Run Code Online (Sandbox Code Playgroud)
由于您使用的是材料组件主题,因此只需使用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()
Run Code Online (Sandbox Code Playgroud)
按钮的默认文本颜色基于此选择器:
<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>
Run Code Online (Sandbox Code Playgroud)
只需检查colorPrimary应用主题中定义的暗模式即可。
如果要更改按钮的文本颜色,可以colorPrimary使用以下方法覆盖:
MaterialAlertDialogBuilder(this, R.style.Theme_MyApp_Dialog_Alert)
Run Code Online (Sandbox Code Playgroud)
和:
<style name="Theme.MyApp.Dialog.Alert" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Text Color for Buttons -->
<item name="colorPrimary">@color/...</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1743 次 |
| 最近记录: |