是否可以为TextView中的不同文本设置多个样式?
例如,我将文本设置如下:
tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);
Run Code Online (Sandbox Code Playgroud)
是否可以为每个文本元素设置不同的样式?例如,line1粗体,word1斜体等.
开发人员指南的常见任务和Android中的操作方法包括选择,突出显示或设置部分文本样式:
Run Code Online (Sandbox Code Playgroud)// Get our EditText object. EditText vw = (EditText)findViewById(R.id.text); // Set the EditText's text. vw.setText("Italic, highlighted, bold."); // If this were just a TextView, we could do: // vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE); // to force it to use Spannable storage so styles can be attached. // Or we could specify that in the XML. // …
我Html.fromHtml用来查看html TextView.
Spanned result = Html.fromHtml(mNews.getTitle());
...
...
mNewsTitle.setText(result);
Run Code Online (Sandbox Code Playgroud)
但Html.fromHtml现在已经在Android N +中弃用了
我/我如何找到新的方法?
private void createStringEndingInRedColor(TextView tv, String word1, String word2) {
Spannable word = new SpannableString(word1);
tv.setText(word);
Spannable wordTwo = new SpannableString(word2);
wordTwo.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(Color.RED)), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(wordTwo);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试为TextView tv编写一个单元测试(使用Robolectric)以确保wordTwo是Color.RED.但是,我只提到了TextView tv.怎么去做这样的任务?
I want a toast with multi-colors. Like this:

I looked at the different tutorials on changing the layout in the xml to create a custom Toast, but none of them explain adding different colors like this.
How can I do this?
================================
ANSWER
================================
Using all your help, I have designed a simple Method() to make color toasts easier to call.
res/layout/toast_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingBottom="6dp"
android:paddingTop="6dp"
android:background="#DAAA"
>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout> …Run Code Online (Sandbox Code Playgroud)