Android Studio中XML布局如何仅给TextView内部分文字设置粗体
纯XML实现TextView部分文字加粗方案
完全可以仅通过XML配置实现需求,不需要修改任何Activity代码,具体操作如下:
- 第一步:在
res/values/strings.xml中定义带HTML粗体标签的字符串资源
其中<string name="test_content">This is a test code, I want to make <b>This</b> word bold</string><b>标签包裹的内容就会显示为粗体,除了粗体标签<b>之外,你还可以用<i>设置斜体、<u>设置下划线,都不需要额外代码支持。 - 第二步:在布局文件的TextView中直接引用该字符串资源即可
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/test_content"/>
如果不想单独定义字符串资源,也可以直接在布局的android:text属性中写转义后的HTML标签,不过可读性较差,不推荐:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a test code, I want to make <b>This</b> word bold"/>
内容的提问来源于stack exchange,提问作者Shaurya Goyal
相关产品推荐
相关产品推荐

