如何为警报对话框文本添加颜色

时间:2018-07-21 11:15:53

标签: android text

我想在警报框中的文本(文件1,文件2 ...文件4)中添加白色,但是它显示的是默认的黑色本身。this is my alert box image 这是我的Java代码

if (storagePermissionGrant()) {

        final Activity activity = this;
        String[] items = {"file1", "file2", "file3", "file4"};
        ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.re);
        AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
        builder.setTitle("Menu")
                .setItems(items, (dialog, which) -> {
                    switch (which) {
                        case 0:
                            MediaUtils.openfile1(activity);
                            break;
                        case 1:
                            MediaUtils.openfile2(activity);
                            break;
                        case 2:
                            MediaUtils.openfile3(activity);
                            break;
                        case 3:
                            MediaUtils.openfile4(activity);
                            break;
                        default:
                            return;
                    }
                });
        builder.show();


    }

这是我的style.xml代码

<style name="re" parent="@android:style/Widget.Holo.ActionBar.Solid">
    <item name="android:background">@color/colorPrimary</item>
    <item name="colorAccent">#fff</item>
    <!--title-->
    <item name="android:textColor">#fff</item>
    <!--text-->
    <item name="android:textColorPrimary">#fff</item>
    <!--selection list-->
    <item name="android:textColorTertiary">#fff</item>
</style>

1 个答案:

答案 0 :(得分:0)

简单来说,您只需使用以下代码更改“ file1”文本:

Html.fromHtml("<font color='#FFFFFF'>file1</font>")

此代码将完全根据您添加的颜色值更改文本颜色。因此,您的字符串数组应如下所示:

String[] items = {
    Html.fromHtml("<font color='#FFFFFF'>file1</font>"), 
    Html.fromHtml("<font color='#FFFFFF'>file2</font>"), 
    Html.fromHtml("<font color='#FFFFFF'>file3</font>"),
    Html.fromHtml("<font color='#FFFFFF'>file4</font>")};