如何在服务器的文本视图中显示图像
How to display images in textview from server
我从服务器文本接收数据,文本中有类似“[LoadImage:ImageLink]”的内容,我希望将此文本替换为 ImageLink 我知道如何从 [=15 下载图像=] 但我不知道如何在文本视图中显示它
我听说过一些关于正则表达式的事情,但我不知道它是如何工作的
(我不想使用 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
}
我已经得到图像了,this答案可能就是您要找的。
本质上,您创建了一个 SpannableString
并将其分配给 setSpan
属性。
这是我的一个旧项目的代码片段,请再次检查!
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();
}
我从服务器文本接收数据,文本中有类似“[LoadImage:ImageLink]”的内容,我希望将此文本替换为 ImageLink 我知道如何从 [=15 下载图像=] 但我不知道如何在文本视图中显示它
我听说过一些关于正则表达式的事情,但我不知道它是如何工作的
(我不想使用 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
}
我已经得到图像了,this答案可能就是您要找的。
本质上,您创建了一个 SpannableString
并将其分配给 setSpan
属性。
这是我的一个旧项目的代码片段,请再次检查!
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();
}