简体   繁体   English

Android Text,文本顶部/底部带有可扩展图像

[英]Android Text with Spannable image on top/bottom of text

I am developing a simple devotional app, which has a Kannada (a language in India) sentence to be displayed. 我正在开发一个简单的灵修应用程序,该应用程序具有要显示的卡纳达语(印度语)。 I am successful in using typeface and displaying the content. 我成功使用了字体并显示了内容。

In few places I have word which has a line on top/bottom of the word as shown below. 在几个地方,我有一个单词,单词的顶部/底部都有一条线,如下所示。 I tried with a spannable image but I am still not able to achieve it properly. 我尝试使用可扩展的图像,但是仍然无法正确实现它。

This is a sample of the code which I am referring to. 这是我所引用的代码示例。 Here I am using a small icon to display it in between the string. 在这里,我使用一个小图标在字符串之间显示它。

Spannable span1 = new SpannableString("The  imageplace");
Drawable android = TestImageActivity.this.getResources().getDrawable(R.drawable.end);
android.setBounds(5, 0, 20, 5);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span1.setSpan(image, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tvTextImage3.setText(span1);

文字上方和下方的细线

ImageSpan extends ReplacementSpan so any characters you are spanning won't get rendered, as the TextLayout is expecting that the span itself will be doing all the rendering. ImageSpan扩展了ReplacementSpan因此您要跨接的任何字符都不会被渲染,因为TextLayout期望跨度本身将完成所有渲染。

What I would recommend is implementing your own ReplacementSpan subclass. 我建议您实现自己的ReplacementSpan子类。 Since it looks like your graphics are associated with one character, you would wrap the single character. 因为看起来您的图形与一个字符相关联,所以您应该包装单个字符。

In the getSize override, you would use start and end to index into text and get the character(s) you are spanning, then use paint.getTextBounds() to measure the width of the text and return that value. getSize覆盖中,您将使用startend来索引text并获取要跨越的字符,然后使用paint.getTextBounds()来测量文本的宽度并返回该值。 You want the width calculation to work in a way that the width of the span doesn't affect the default spacing of the text. 您希望宽度计算以某种方式起作用,即跨度的宽度不影响文本的默认间距。

Another thing this method might need to do is change the FontMetrics by increasing the ascent and descent in order to give you some space to draw the lines. 此方法可能需要做的另一件事是通过增加上升和下降来更改FontMetrics ,以便给您一些空间来绘制线条。

In the draw override, you use the paint to render the text that isn't being rendered within the span. draw替代中,您可以使用paint来呈现未在跨度内呈现的文本。 The paint and font metrics should already have the proper values so that your text render looks like the surrounding text. 绘画和字体指标应该已经具有适当的值,以便您的文本呈现看起来像周围的文本。 Of course, you'll also render the line graphics you want. 当然,您还将渲染所需的线图形。

For some sample code, take a look at my answer to a similar question . 对于一些示例代码,请看一下我对类似问题的回答 This has all the pieces I just discussed. 这包含了我刚才讨论的所有内容。

If you want me to write some code for this, you'll need to provide some code that gives me a starting point with some actual Kannada text along with what the lines are and where they go. 如果要我为此编写一些代码,则需要提供一些代码,让我从实际的卡纳达语文本以及行的含义和行进位置着手。 I don't even know if Kannada text is LTR or RTL; 我什至不知道卡纳达语文字是LTR还是RTL。 that might affect how the span subclass is coded. 这可能会影响span子类的编码方式。 Preferably the text would correspond to the image you posted so I can see how it should look when it's working. 文字最好与您发布的图像相对应,以便我可以看到其工作时的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM