-1
4

5 回答 5

3
Actually the issue was, in the String resource if we write `<b>` its will be converted to string itself.
And when Html.fromHtml() tries to convert they found no tags,
So,

For < we have to use  &lt;
For > we have to use  &gt;
For & we have to use  &amp;

将文本检索为跨区文本并将其传递给 drawtext()。有效 !!无论如何感谢您的努力。

于 2018-01-24T12:43:06.527 回答
2

您可以通过将特定字符的 pint 样式设置为 BOLD 以及字符串的开始和结束索引(即粗体)来做到这一点,您可以这样做 -

假设字符串是

<string name="bold_text">This is the Bold text</string>

以下是要在视图类中完成的

//create two paints one is regular and another is bold
private Paint mPaintText;
private Paint mPaintTextBold;

private String textToDraw;

// initialize them
mPaintText = new Paint();
mPaintText.setColor(Color.WHITE);
mPaintText.setStyle(Style.FILL);
mPaintText.setTextSize(32f);

mPaintTextBold= new Paint();
mPaintTextBold.setColor(Color.WHITE);
mPaintTextBold.setStyle(Style.FILL);
mPaintTextBold.setTextSize(32f);
mPaintTextBold.setTypeface(Typeface.DEFAULT_BOLD);

textToDraw = getString(R.string.bold_text);

// Now in on draw method of view draw the following text if you are drawing 
// text on canvas it means you already have start point let it be be 
// startX and startY, index of the bold string be boldStart and boldEnd in 
// our case it will be 12 and 16

String normalStartString = mTextToDraw.substring(0, boldStart);
String normalEndString = mTextToDraw.substring(boldEnd);
String boldString = mTextToDraw.substring(boldStart, boldEnd);

Paint.FontMetrics fm = mPaintText.getFontMetrics();
float height = -1 * (fm.ascent + fm.descent);

// drawing start string
canvas.drawText(normalStartString, startX, startY - height, mPaintText);

// drawing bold string
float width = mPaintText.measureText(normalStartString) + startX;
canvas.drawText(boldString, width, startY - height, mPaintTextBold);

// drawing end string
width += mPaintTextBold.measureText(boldString);
canvas.drawText(normalEndString, width, startY - height, mPaintText);
于 2018-01-31T05:56:55.223 回答
-1

你可以使用WebView,这支持的标签比Html.fromHtml().

yourWebView.loadData("<b>Hello bold Text</b>", "text/html", "utf-8");

https://developer.android.com/reference/android/webkit/WebView.html

于 2017-12-27T06:00:19.580 回答
-1

这可以通过两种方式实现。在 strings.xml 中

<string name="bold_text">This text is <b>Bold</b></string>

and in your activity xml file

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bold_text" />

在 Java 代码中

String htmlAsString = getString(R.string.bold_text);
textView.setText(Html.fromHtml(htmlAsString));
于 2017-12-27T06:07:31.230 回答
-1

strings.xml内的values文件夹中,您可以使用如下标签:

<string name="string_hello"><b>Hello</b></string>

并在您的活动文件中使用。

your_textView_or_anything.setText(R.string.string_hello);
于 2017-12-27T06:08:04.977 回答