如何修改Android AlertDialog选项列表的文字颜色为白色?
解决AlertDialog选项文字无法改为白色的问题
方案一:修改自定义主题属性
在你的MyAlertDialogStyle主题中,添加专门控制AlertDialog列表项文字颜色的属性:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="android:windowBackground">@drawable/dialog_background</item> <item name="android:textColorPrimary">@color/white</item> <item name="android:textColorSecondary">@color/blue</item> <!-- 添加这行,直接指定列表项文字颜色 --> <item name="android:textColorAlertDialogListItem">@color/white</item> </style>
这个属性是Android系统专门用于设置AlertDialog列表项文本颜色的,优先级高于全局文本颜色属性,能直接覆盖默认的黑色文字。
方案二:自定义列表项布局(兼容性更强)
如果主题修改仍不生效,可以直接自定义列表项布局,完全掌控文字样式:
- 创建
dialog_list_item.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:textColor="@color/white" android:textSize="16sp" />
- 修改Kotlin代码,用
setAdapter替代setItems:
@SuppressLint("CutPasteId") private fun showPageSelectionDialog() { val options = arrayOf("A4", "A5") val builder = AlertDialog.Builder(this, R.style.MyAlertDialogStyle) builder.setTitle("Select PDF size") .setAdapter(ArrayAdapter(this, R.layout.dialog_list_item, options)) { _, which -> val selectedPageSize = if (which == 1) PageSize.A5 else PageSize.A4 pdf(name.text.toString().trim(), selectedPageSize) } .setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() } builder.create().show() }
这种方式完全绕过系统默认的列表项样式,直接用自定义布局控制文字颜色,适配所有Android版本。
补充检查
确保你的colors.xml中正确定义了白色:
<color name="white">#FFFFFF</color>
内容的提问来源于stack exchange,提问作者Hugo Jiménez
相关产品推荐
相关产品推荐

