测试
<resources>
<string name="app_name">MyApp</string>
<string name="str1">恭喜您!您的手机跑分为<font color='#F50057'><big><big><big>888888分</big></big></big></font>,已经超过全国<font color='#00E676'><big><big><big>99%</big></big></big></font>的Android手机。</string>
<string name="str2"><p>(1).Name:Toking Hazard by Joking Hazard</p><p>(2).Material: Paper</p><p>(3).Package: Box</p><p><br /></p><p>50 Marijuana themed cards to heighten your Joking Hazard experience.<br /></p><p>This is an expansion pack. It requires Joking Hazard to play</p><p>In addition to the cards, there is a secret in each box!</p><p>The box is OVERSIZED to fit the surprise</p><p><br /></p></string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:text=""
android:textColor="@android:color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/black"
android:text=""
android:textColor="@android:color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/black"
android:text=""
android:textColor="@android:color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/black"
android:text=""
android:textColor="@android:color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/black"
android:text=""
android:textColor="@android:color/white"
android:textSize="13sp" />
<TextView
android:id="@+id/tv6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@android:color/black"
android:text=""
android:textColor="@android:color/white"
android:textSize="13sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val content1 =
"恭喜您!您的手机跑分为<font color='#F50057'><big><big><big>888888分</big></big></big>" +
"</font>,已经超过全国<font color='#00E676'>" +
"<big><big><big>99%</big></big></big></font>的Android手机。"
val content2 =
"<p>(1).Name:Toking Hazard by Joking Hazard</p><p>" +
"(2).Material: Paper</p><p>" +
"(3).Package: Box</p><p><br/></p>" +
"<p>50 Marijuana themed cards to heighten your Joking Hazard experience.<br/></p>" +
"<p>This is an expansion pack. It requires Joking Hazard to play</p>" +
"<p>In addition to the cards, there is a secret in each box!</p>" +
"<p>The box is OVERSIZED to fit the surprise</p><p><br/></p>"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//FROM_HTML_MODE_COMPACT:html 块元素之间使用一个换行符分隔
tv1.text = Html.fromHtml(content1, Html.FROM_HTML_MODE_COMPACT)
//FROM_HTML_MODE_LEGACY:html 块元素之间使用两个换行符分隔
tv2.text = Html.fromHtml(content1, Html.FROM_HTML_MODE_LEGACY)
//FROM_HTML_MODE_COMPACT:html 块元素之间使用一个换行符分隔
tv4.text = Html.fromHtml(content2, Html.FROM_HTML_MODE_COMPACT)
//FROM_HTML_MODE_LEGACY:html 块元素之间使用两个换行符分隔
tv5.text = Html.fromHtml(content2, Html.FROM_HTML_MODE_LEGACY)
} else {
tv1.text = Html.fromHtml(content1)
tv2.text = Html.fromHtml(content1)
tv4.text = Html.fromHtml(content2)
tv5.text = Html.fromHtml(content2)
}
//来自string
tv3.text = resources.getString(R.string.str1)
tv6.text = resources.getString(R.string.str2)
}
}
效果图:
结论:
来自string资源的html文本没有任何效果
Html.fromHtml(“”, Html.FROM_HTML_MODE_COMPACT) : html 块元素之间使用一个换行符分隔
Html.fromHtml(“”, Html.FROM_HTML_MODE_LEGACY) : html 块元素之间使用两个换行符分隔
注意:
public final void setText(CharSequence text) {
setText(text, mBufferType);
}
Html.fromHtml 之后不要tostring,否则html效果会有问题 , setText调用setText(CharSequence text)才是正确的。
public static Spanned fromHtml(String source, int flags) {
return fromHtml(source, flags, null, null);
}
public interface Spanned
extends CharSequence
{}