Android 使用<;b>;在字符串资源中添加标记,以放入canvas drawtext()方法

Android 使用<;b>;在字符串资源中添加标记,以放入canvas drawtext()方法,android,string,spanned,Android,String,Spanned,在安卓系统中,我想在段落中加粗特定文本。我将该段落存储在项目的字符串资源中,现在我需要将该字符串用作Canvas的参数drawtext()方法。 其中,它将绘制指定的字符串,并在我使用标记将其指定为bold的位置将文本设置为粗体 我尝试添加粗体标记并使用Html检索它。fromHtml(),但没有成功,使用了gettext()和getString() 我还是找不到答案。我希望它是一个字符串,但不是放在文本视图中。我尝试了跨文本或字符序列 请给我一个解决方案。您可以使用WebView,它支持的标签

在安卓系统中,我想在段落中加粗特定文本。我将该段落存储在项目的字符串资源中,现在我需要将该字符串用作Canvas的参数drawtext()方法。 其中,它将绘制指定的字符串,并在我使用标记
将其指定为bold的位置将文本设置为粗体

我尝试添加粗体标记
并使用
Html检索它。fromHtml()
,但没有成功,使用了
gettext()
getString()

我还是找不到答案。我希望它是一个字符串,但不是放在
文本视图中
。我尝试了
跨文本
或字符序列
请给我一个解决方案。

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

yourWebView.loadData(“Hello粗体文本”、“Text/html”、“utf-8”);

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

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

<string name="string_hello"><b>Hello</b></string>
实际上问题是,在字符串资源中,如果我们写``它将被转换为字符串本身。
当Html.fromHtml()尝试转换时,他们没有发现任何标记,
所以
因为我们必须使用
对于>我们必须使用
因为&我们必须使用&;

以跨距文本形式检索文本并将其传递给drawtext()。成功了!!无论如何,感谢您的努力。

您可以通过将特定字符的品脱样式设置为粗体来完成此操作,并且字符串的开始索引和结束索引(即粗体)也应为已知。您可以按以下方式完成此操作:-

假设字符串是

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

可能重复的,因为您正在画布上使用
drawText
,所以无法使用标记。但是你可以尝试使用
Paint
对象。@我特别提到我没有使用textview,在textview中这样做很简单。请不要说所有内容都是重复的?这就是为什么你看到一个词说“可能”。它不起作用。我已经试过了,并且提到了我尝试过的所有方法。我想在canvas drawtext()方法中使用它,而不是在textview的settext中
<string name="string_hello"><b>Hello</b></string>
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;
<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);