如何在android中的类文件中加粗文本?

时间:2013-04-04 10:21:45

标签: android

 String  _body = "sq Home : "+HomeScreen.edt_sqft_home.getText().toString() +"\n"
                        +"Age of Home years : "+HomeScreen.edt_age.getText().toString() +"\n"
                        +"Additional service : "+HomeScreen.output +"\n\n"
                        +"Street Name : "+HomeScreen.edt_streetname.getText().toString() +"\n"
                        +"City : "+HomeScreen.edt_city.getText().toString() +"\n"
                        +"State : "+HomeScreen.edt_state.getText().toString();

我希望用“”中的粗体文字。比如“Sq Home”,“家庭年龄”。 它怎么可能?请帮助我

2 个答案:

答案 0 :(得分:0)

String source = "This is example text";
Spannable out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

答案 1 :(得分:0)

如果您的字符串将在TextView中显示,则可以使用HTML标记。 在您的情况下,您只需要这样做:

String _body = "<b>sq Home</b>: " + HomeScreen.edt_sqft_home.getText().toString() + "<br/>"
       + "<b>Age of Home years</b>: " + HomeScreen.edt_age.getText().toString() + "<br/>"
       + "<b>Additional service</b>: " + HomeScreen.output + "<br/><br/>"
       + "<b>Street Name</b>: " + HomeScreen.edt_streetname.getText().toString() + "<br/>"
       + "<b>City</b>: " + HomeScreen.edt_city.getText().toString() + "<br/>"
       + "<b>State</b>: " + HomeScreen.edt_state.getText().toString();

TextView yourTextView = findViewById(R.id.yourTextViewId);
yourTextView.setText(Html.fromHtml(_body));

其中yourTextView是显示HTML字符串的Activity视图。

Mark Murphy用HTML tags supported by TextView写了一篇文章。