Android的帐篷,每个字母都有不同的颜色 [英] Android marquee where each letter has different color

查看:199
本文介绍了Android的帐篷,每个字母都有不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的TextView选取框(一个的LinearLayout内)。我需要每个字母有不同的颜色。有谁知道我怎么能做到这一点?该文本是pre定义的,所以我可以硬code一切如果需要的话。这里是我的TextView

I have a simple TextView marquee (inside a LinearLayout). I need each letter to have a different color. Does anyone know how I could accomplish this? The text is pre-defined so I can hardcode everything if needs be. Here is my TextView

  <TextView
        style="@style/myFont"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.45"
        android:ellipsize="marquee"
        android:lines="1"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="@string/my_text" />

说文字是世界,你好!我快乐!

Say the text is "Hello world! I am happy!"

推荐答案

使用的 Html.fromHtml()并设置文本。

randomColor = http://stackoverflow.com/a/4246400/1008278

randomColor = http://stackoverflow.com/a/4246400/1008278

在XML的颜色列表>> https://gist.github.com/VenomVendor/6857539修改为字符串数组

List of colors in XML >> https://gist.github.com/VenomVendor/6857539 modify to StringArray

    String tempText = "Hello world! I am happy!"
    String[] tempTextArr = string.split("");
    String newText = "";
    String[] colors = getResources().getStringArray(R.array.color_n);

    for(int i = 0; i < tempTextArr.length; i++ )
    {
        if(!tempTextArr[i].equals(" "))
        {
            newText = newText + "<font color='"+colors[i]+"'>"+tempTextArr[i]+"</font>"
            //newText = newText + "<font color='"+randomColor+"'>"+tempTextArr[i]+"</font>"
        }
        else{
            newText = newText + " ";
        }
    }

    tv.setText(Html.fromHtml(newText));

这篇关于Android的帐篷,每个字母都有不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆