BOLD和普通文本中的Android TextView

8 android android-layout

我彻底搜索了SO,但没有得到我的问题的答案.

我想设置一个段落,我将使用XML设置它

该文本包含标题和步骤以及常规文本.我想以粗体显示标题和步骤,并在普通文本中休息.

我可以通过使用不同的,但我怎么能在同一TextView中做到这一点.

我的意思是使用相同的TextView如何为不同的句子设置不同的属性?

Rag*_*dan 25

使用Spannable字符串

 TextView tv = (TextView) findViewById(R.id.tv);  
 String steps = "Hello Everyone";
 String title="Bold Please!";

 SpannableString ss1=  new SpannableString(title);
 ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0); 
 tv.append(ss1);
 tv.append("\n");
 tv.append(steps);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有关更多样式,请查看链接@ http://blog.stylingandroid.com/archives/177


Aru*_*n C 15

在你的字符串文件中

<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content  content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content  content content content  content content content</p></p>
]]>
</string>
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中

        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(Html.fromHtml(getString(R.string.your_text)));
Run Code Online (Sandbox Code Playgroud)

并输出

在此输入图像描述