将HTML标签的android TextView的到 [英] Apply html tags to textview in android
本文介绍了将HTML标签的android TextView的到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我显示从sqlite的DB数据。在数据库中的数据存储在HTML格式。数据中包含的图像的标签,以显示图像。我能够显示数据,但我怎么能与它一起展示的形象呢?我的图像被存储在服务器中。
我的code:
<$p$p><$c$c>((TextView)view.findViewById(R.id.tv_text)).setText(Html.fromHtml(listItem.gettext()+\"\"));从上面的线,我显示从数据库中的数据。现在,随着沿我需要显示这是在服务器中的图像。我怎样才能做到这一点?
解决方案
我尝试code。希望它能帮助
的TextView mDetail;
进度mProgressBar;
绳体;
处理程序处理程序;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.act_recipe_detail);
getActionBar()setDisplayHomeAsUpEnabled(真)。 如果(this.getIntent()。getStringExtra(身体)!= NULL)
体= this.getIntent()getStringExtra(身体)。 mDetail =(TextView中)this.findViewById(R.id.tv_recipe_detail); 如果(this.getIntent()。getStringExtra(身体)!= NULL)
体= this.getIntent()getStringExtra(身体)。 mDetail.setText(Html.fromHtml(体)); mProgressBar =(进度)this.findViewById(R.id.bar);
mDetail.setMovementMethod(ScrollingMovementMethod.getInstance()); //皛
处理器=新的处理程序(){
@覆盖
公共无效的handleMessage(消息MSG){
// TODO自动生成方法存根
如果(msg.what == 0x101){
mProgressBar.setVisibility(View.GONE);
mDetail.setText((CharSequence的)msg.obj);
}
super.handleMessage(MSG);
}
};
线程t =新主题(新ImageTask());
t.start();
mProgressBar.setVisibility(View.VISIBLE);}
类ImageTask实现Runnable {
消息味精= Message.obtain(); @覆盖
公共无效的run(){ ImageGetter imageGetter =新ImageGetter(){
@覆盖
公众可绘制getDrawable(字符串源){
// TODO自动生成方法存根
URL网址;
可绘制可绘制= NULL;
尝试{
URL =新的URL(源);
绘制= Drawable.createFromStream(
url.openStream(),NULL); INT scale_y = MAX_HEIGHT / drawable.getIntrinsicHeight();
drawable.setBounds(0,0,
drawable.getIntrinsicWidth()* scale_y,
drawable.getIntrinsicHeight()* scale_y); }赶上(MalformedURLException的E){
Log.e(TAGMalformedURLException的+ e.toString());
}赶上(IOException异常五){
Log.e(TAG,IOException异常+ e.toString());
}
返回绘制;
}
};
CharSequence的测试= Html.fromHtml(身体,imageGetter,NULL);
msg.what = 0x101;
msg.obj =测试;
handler.sendMessage(MSG);
}
}
I am displaying data from sqlite db. The data in the database is stored in html. The data contains an image tag to display an image. I am able to display the data but how can I display the image along with it? My images are stored in the server.
My Code:
((TextView)view.findViewById(R.id.tv_text)).setText(Html.fromHtml(listItem.gettext()+""));
From the above line I am displaying the data from database. Now along with that I need to display the image which is in server. How can I do this?
解决方案
Try my code . Hope it helps
TextView mDetail;
ProgressBar mProgressBar;
String body;
Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_recipe_detail);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (this.getIntent().getStringExtra("body") != null)
body = this.getIntent().getStringExtra("body");
mDetail = (TextView) this.findViewById(R.id.tv_recipe_detail);
if (this.getIntent().getStringExtra("body") != null)
body = this.getIntent().getStringExtra("body");
mDetail.setText(Html.fromHtml(body));
mProgressBar = (ProgressBar) this.findViewById(R.id.bar);
mDetail.setMovementMethod(ScrollingMovementMethod.getInstance());// 皛
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 0x101) {
mProgressBar.setVisibility(View.GONE);
mDetail.setText((CharSequence) msg.obj);
}
super.handleMessage(msg);
}
};
Thread t = new Thread(new ImageTask());
t.start();
mProgressBar.setVisibility(View.VISIBLE);
}
class ImageTask implements Runnable {
Message msg = Message.obtain();
@Override
public void run() {
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
URL url;
Drawable drawable = null;
try {
url = new URL(source);
drawable = Drawable.createFromStream(
url.openStream(), null);
int scale_y = MAX_HEIGHT/drawable.getIntrinsicHeight();
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth()*scale_y,
drawable.getIntrinsicHeight()*scale_y);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException" + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException" + e.toString());
}
return drawable;
}
};
CharSequence test = Html.fromHtml(body, imageGetter, null);
msg.what = 0x101;
msg.obj = test;
handler.sendMessage(msg);
}
}
这篇关于将HTML标签的android TextView的到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文