我正在制作一个应用程序,在该应用程序中我想制作一些部分粗体部分正常的文本,如下所示:
蛋糕试试这个用胡萝卜做的美味蛋糕。
在我的 main.xml 中,我输入了这个:
<LinearLayout
android:id="@+id/itemone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:padding="3dp" >
<ImageView
android:layout_width="110dp"
android:layout_height="85dp"
android:src="@drawable/cake" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cake"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
在我的 strings.xml 中我输入了这个:
<string name="cake"><b>Cake</b>\n Try this delicious cake made with carrots.</string>
结果不是我除的。我得到的只是“蛋糕”这个词,而且它不是粗体。
最佳答案
textAppearanceSmall 搞砸了这件事。
它以此为风格,忽略其他。不要包括它。
还有一些东西搞砸了,标签“<”和“>”应该被替换,因为有特殊的 xml 字符。搜索 html 转义字符或小数字符,< 应该如下所示:& l t ; (一个字)
如果你觉得无聊,你可以在 xml 中搜索 CDATA。有一种方法可以在 xml 中包含纯文本。
无需动态设置TextView的文本。
关于xml 中的 Android 粗体文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11334129/