Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中动态设置TextView的颜色_Java_Android_Textview - Fatal编程技术网

Java 如何在Android中动态设置TextView的颜色

Java 如何在Android中动态设置TextView的颜色,java,android,textview,Java,Android,Textview,在我的应用程序中,我想在TextView中显示一些文本,我从服务器获取这些文本 我应该在XML布局中只使用一个textView,我应该为这个文本设置颜色 我的XML: <TextView android:id="@+id/rowExplore_userName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft=

在我的应用程序中,我想在
TextView
中显示一些文本,我从服务器获取这些文本
我应该在XML布局中只使用一个
textView
,我应该为这个文本设置颜色
我的XML:

<TextView
    android:id="@+id/rowExplore_userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/size3"
    android:layout_marginTop="@dimen/padding8"
    android:layout_toRightOf="@+id/rowExplore_imageCard"
    android:fontFamily="sans-serif"
    android:gravity="left|center_vertical"
    android:text="Adam"
    android:textColor="@color/colorPrimary"
    android:textSize="@dimen/font14" />
  "name": "Adam Sandel",
  "info": "liked",
  "movieName": "SpiderMan",
rowExplore_userName.setText(response.getName() + response.getInfo() + response.getMovieName() );
Java代码:

<TextView
    android:id="@+id/rowExplore_userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/size3"
    android:layout_marginTop="@dimen/padding8"
    android:layout_toRightOf="@+id/rowExplore_imageCard"
    android:fontFamily="sans-serif"
    android:gravity="left|center_vertical"
    android:text="Adam"
    android:textColor="@color/colorPrimary"
    android:textSize="@dimen/font14" />
  "name": "Adam Sandel",
  "info": "liked",
  "movieName": "SpiderMan",
rowExplore_userName.setText(response.getName() + response.getInfo() + response.getMovieName() );
我希望动态设置颜色,如下图所示:

我该怎么做?请帮助我你可以使用HTML

String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
String text=“第一种颜色第二种颜色”;
yourtextview.setText(Html.fromHtml(text));

更好的方法是使用适当的HTML标记创建字符串资源:

<string name="html_string" formatted="false">
    <![CDATA[
       <font color="yellow">%s</font> %s <font color="yellow">%s</font>
    ]]>
</string>
我使用了以下代码:

String colorStr="#FFFFFF" //put your hex color 
int color =  Color.parseColor(colorStr);
editText.setTextColor(color);

如果要避免使用HTML标记,可以使用
SpannableString

TextView textview = (TextView)findViewById(R.id.textview);

Spannable wordToSpan = new SpannableString("Your text is here");        
wordToSpan.setSpan(new ForegroundColorSpan(Color.BLUE), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(wordToSpan);

而且它更灵活,因为您可以对文本进行更多更改(文本大小、粗体/斜体等)

我有另一种方法,只需创建一种方法

 public String getColoredString(String pname) {
    Random rnd = new Random();
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    Spannable wordToSpan = new SpannableString(pname);
    wordToSpan.setSpan(new ForegroundColorSpan(color), 0, pname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return wordToSpan.toString();
}
并使用上述方法

rowExplore_userName.setText(getColoredString(response.getName())+getColoredString(response.getInfo())+getColoredString(response.getMovieName()));
这应该行得通。

我认为这将有助于您: