Android:设置提醒对话框中单选按钮的文本颜色

c0vxltue  于 2023-06-04  发布在  Android
关注(0)|答案(4)|浏览(379)

我有一个简单的警报对话框与单一的选择项目

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>

当我运行应用程序时,我看到此警报

有没有办法在不创建自定义布局的情况下更改单选按钮附近文本的颜色?

eblbsuwk

eblbsuwk1#

为单选按钮项着色的样式

<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>
5tmbdcev

5tmbdcev2#

你可以做得很有格调

<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)
shyt4zoc

shyt4zoc3#

试试这个

val array = arrayOf(Html.fromHtml("<font color='#00FF00'>aa</font>"),
                Html.fromHtml("<font color='#00FF00'>bb</font>"),
                Html.fromHtml("<font color='#00FF00'>cc</font>"))

您可以查看更多细节here

v1l68za4

v1l68za44#

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>"

相关问题