在字符串资源 android 中使用 <b> 标签放入 canvas drawtext() 方法

Using <b> tag inside string resource android to put in canvas drawtext() method

在 Android 中,我想将段落中的特定文本加粗。我将该段落存储在 project.Now 的字符串资源中,我需要将该字符串用作 Canvas、drawtext() 方法的参数。 它将在其中绘制指定的字符串并在我使用标记 <b>.

将其指定为 Bold 的地方将文本设为粗体

我尝试添加粗体标签 <b> 并使用 Html.fromHtml(_) 检索它,但没有成功,使用了 gettext()getString()

我还是找不到答案。我想要它作为一个字符串,但不要放入 text view。我试过 spanned text 或字符序列 请给我一个解决方案。

您可以使用 WebView,这比 Html.fromHtml() 支持更多的标签。

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

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

这可以通过两种方式实现。在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));

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

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

并在您的 Activity 文件中使用。

your_textView_or_anything.setText(R.string.string_hello);
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()。有效 !!无论如何,谢谢你的努力。

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

假设字符串是

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

以下是视图中要做的事情class

//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);