如何在不同应用主题下强制对话框按钮使用colorAccent?
问题描述
我原本期望对话框按钮文本使用colorAccent,但测试发现不同Android主题下表现不一致:
Theme.AppCompat.Light.DarkActionBar:对话框按钮文本使用colorAccent(符合预期)Theme.MaterialComponents.DayNight.DarkActionBar:对话框按钮文本使用colorPrimaryTheme.Material3.DayNight.NoActionBar:对话框按钮文本使用colorPrimary
需求:在任意应用主题下,强制对话框按钮文本使用colorAccent。
测试代码
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <!-- Theme.Material3.DayNight.NoActionBar = Dialog button use colorPrimary --> <!-- Theme.MaterialComponents.DayNight.DarkActionBar = Dialog button use colorPrimary --> <!-- Theme.AppCompat.Light.DarkActionBar = Dialog button use colorAccent --> <style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight.NoActionBar"> <item name="colorPrimary">#ff0000</item> <!-- red --> <item name="colorAccent">#ff1778f2</item> <!-- blue --> </style> <style name="Theme.MyApplication" parent="Base.Theme.MyApplication" /> </resources>
MainActivity.kt
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) showDialog() } private fun showDialog() { val builder = AlertDialog.Builder(this) builder.setTitle("Confirmation") builder.setMessage("Do you want to proceed?") // Add OK button builder.setPositiveButton("OK") { dialog, which -> // Handle OK button click here } // Add Cancel button builder.setNegativeButton("Cancel") { dialog, which -> // Handle Cancel button click here dialog.dismiss() } // Create and show the AlertDialog val alertDialog = builder.create() alertDialog.show() } }
解决方案
方案1:全局主题配置(推荐)
不同Android主题体系(AppCompat、Material Components、Material3)对应的对话框按钮样式属性不同,通过在应用主题中覆盖对应样式,强制按钮文本使用colorAccent。
修改themes.xml,在基础主题中添加按钮样式覆盖:
<style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight.NoActionBar"> <item name="colorPrimary">#ff0000</item> <!-- 红色 --> <item name="colorAccent">#ff1778f2</item> <!-- 蓝色 --> <!-- 覆盖对话框所有按钮样式 --> <item name="buttonBarPositiveButtonStyle">@style/DialogAccentButton</item> <item name="buttonBarNegativeButtonStyle">@style/DialogAccentButton</item> <item name="buttonBarNeutralButtonStyle">@style/DialogAccentButton</item> </style> <!-- 定义对话框按钮样式,文本颜色绑定colorAccent --> <style name="DialogAccentButton" parent="Widget.Material3.Button.TextButton.Dialog"> <item name="android:textColor">?attr/colorAccent</item> </style>
- 若使用Material Components主题(非Material3),将
DialogAccentButton的父样式改为Widget.MaterialComponents.Button.TextButton.Dialog - 若使用AppCompat主题,将父样式改为
Widget.AppCompat.Button.ButtonBar.AlertDialog
配置完成后,所有通过AlertDialog.Builder创建的对话框按钮都会自动使用colorAccent。
方案2:代码动态设置按钮颜色
如果不需要全局修改,可在对话框显示后手动设置按钮文本颜色:
修改MainActivity.kt中的showDialog方法:
private fun showDialog() { val builder = AlertDialog.Builder(this) builder.setTitle("Confirmation") builder.setMessage("Do you want to proceed?") builder.setPositiveButton("OK") { dialog, which -> // 处理OK点击逻辑 } builder.setNegativeButton("Cancel") { dialog, which -> dialog.dismiss() } val alertDialog = builder.create() alertDialog.show() // 获取主题中定义的colorAccent val typedValue = TypedValue() theme.resolveAttribute(R.attr.colorAccent, typedValue, true) val accentColor = typedValue.data // 为所有按钮设置颜色 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)?.setTextColor(accentColor) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE)?.setTextColor(accentColor) alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL)?.setTextColor(accentColor) }
内容的提问来源于stack exchange,提问作者Cheok Yan Cheng
相关产品推荐
相关产品推荐

