Android:在警告对话框中设置单选按钮的文本颜色

4

我有一个简单的警告对话框,其中包含单选项

val alertDialog = AlertDialog.Builder(this)
alertDialog.setTitle("My Title")
val array = arrayOf("aa", "bb", "cc")
alertDialog.setSingleChoiceItems(array, 0) { _, selectedItem ->
}
alertDialog.setNegativeButton("Cancel") { _, _ ->
}
alertDialog.create()
alertDialog.show()

我已经为我的主题添加了自定义的警告对话框样式。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

     .........
     <item name="android:timePickerDialogTheme">@style/DefaultAlertDialogTheme</item>
     <item name="android:datePickerDialogTheme">@style/DefaultAlertDialogTheme</item>
     <item name="android:alertDialogTheme">@style/DefaultAlertDialogTheme</item>
</style>

这是我的样式

<style name="DefaultAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">@color/greenButtonBackgroundColor</item>
    <item name="android:textColorSecondary">@color/greenButtonBackgroundColor</item>
    <!--title-->
    <item name="android:textColor">@color/toolbarTitleTextColor</item>
</style>

当我运行应用程序时,我看到这个警报。

enter image description here

有没有办法在不创建自定义布局的情况下更改单选按钮旁边文本的颜色?
4个回答

7

样式化单选按钮选项的颜色

<style name="DefaultAlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <!--        Title Color-->
    <item name="android:textColorPrimary">@color/black</item>
    <!--        Radio button unchecked-->
    <item name="android:colorControlNormal">@color/medium_gray</item>
    <!--        Radio button checked-->
    <item name="android:colorControlActivated">@color/warmPink</item>
    <!--        List item on click highlight color-->
    <item name="android:colorControlHighlight">@color/transparent</item>
</style>

1
它有效... 如果你想同时改变单选钮文字颜色,加入"<item name="textColorAlertDialogListItem">@color/color_detect_button_color</item>" - Ucdemir

2

你可以以时尚的方式做到它

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">                      
    <item name="android:textColor">@color/your_text_color_here</item>       
</style>

然后在你的代码中这样做

val alertDialog = AlertDialog.Builder(this, R.style.MyDialogTheme)

1
实际上这是对我有效的:<item name="android:textColorAlertDialogListItem">@color/your_text_color_here</item> - MasterMind

1
尝试这个。
 val array = arrayOf(Html.fromHtml("<font color='#00FF00'>aa</font>"),
                Html.fromHtml("<font color='#00FF00'>bb</font>"),
                Html.fromHtml("<font color='#00FF00'>cc</font>"))

你可以在这里查看更多细节

0
Add into Style to change the color of radio button items

<style name="DefaultAlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:colorControlNormal">@color/gray</item
    <item name="android:colorControlActivated">@color/blue</item>
    <item name="android:colorControlHighlight">@color/light_gray</item>
</style>

If you need to change radiobutton text color add this in your styles" 

    <item name="textColorAlertDialogListItem">@color/color_white</item>"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接