我使用 Html.fromHtml 。它在API 21&更高但在低API 21中不显示粗体。我只想做粗体 R.string.str_taker_name (NOT: edtText.getName())。如何为每个API加粗此文本。
takerName= (TextView) findViewById(R.id.txt_taker_name);
takerName.setText(Html.fromHtml("<b>"+getResources().getString(R.string.str_taker_name) +"</b>: "+ edtText.getName()));
答案 0 :(得分:1)
你可以试试这个:
TextView txt_taker_name = (TextView) findViewById(R.id.myText);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
txt_taker_name.setTypeface(boldTypeface);
或者在xml中设置属性:
android:textStyle="bold"
在这种情况下,您可以尝试此功能:
public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){
SpannableStringBuilder builder=new SpannableStringBuilder();
if(textToBold.length() > 0 && !textToBold.trim().equals("")){
String testText = text.toLowerCase(Locale.US);
String testTextToBold = textToBold.toLowerCase(Locale.US);
int startingIndex = testText.indexOf(testTextToBold);
int endingIndex = startingIndex + testTextToBold.length();
if(startingIndex < 0 || endingIndex <0){
return builder.append(text);
}
else if(startingIndex >= 0 && endingIndex >=0){
builder.append(text);
builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0);
}
}else{
return builder.append(text);
}
return builder;
}
并使用它:
YourActivity.makeSectionOfTextBold(edtText.getName(), getResources().getString(R.string.str_taker_name));
并将结果设置为TextView
希望它有所帮助!!!