我从服务器文本接收数据,在文本中有类似'[LoadImage:ImageLink]'的内容,我希望用ImageLink替换此文本我知道如何从链接下载图像,但我不知道知道如何在textview中显示它
我听过有关正则表达的事情,但我不知道它是如何工作的
(我不想使用ImageSpan,我想将该文本([LoadImage:ImageLink])替换为图像)
我在一个论坛中发现了这个但是还没有完成(我想要这样的东西)
String text = "This is An Image File [LoadImage:'image1.png'] this is Another Image [LoadImage:'image2.png']";
Pattern pattern = Pattern.compile("\\[LoadImage:(.*?)\\]")
ProccessText( Text, layoutRoot, R.layout.image_style, R.layout.text_style);
private void processText(String text, LinearLayout layoutRoot,int imageLayout, int textLayout) {
// proccess input Text Here !
// i don't know what should i write here
}
答案 0 :(得分:1)
这是我的一个旧项目的代码段,所以请再次检查一下!
while (matcher.find()) {
try {
// Maybe it's Text Before Image
String textBeforImage = text.substring(offset, matcher.start());
offset += textBeforImage.length();
textBeforImage = textBeforImage.trim();
if (textBeforImage.length() != 0) {
addTextView(textBeforImage);
}
// now , if it's Text before founded image it's Handled ! ,so add Image !
String ImageName = matcher.group(1).toString();
if (ImageName.length() != 0) {
addImageView(ImageName);
offset += text.substring(matcher.start(), matcher.end()).length();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
try {
//Text After Last Image
if (offset != text.length()) {
String content = text.substring(offset).trim();
if (content.length() != 0) {
addTextView(content);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
答案 1 :(得分:0)
我已经收到了图片,this回答可能就是您要找的。 p>
基本上,您创建了一个SpannableString
并将其分配给setSpan
属性。