设为首页 加入收藏

TOP

Android中显示html标签或者带图片
2014-11-24 11:30:41 来源: 作者: 【 】 浏览:0
Tags:Android 显示 html 标签 或者 图片

只显示带文本的html可以用下面的方法处理html文件。


public static Spanned fromHtml (String source)
显示带图片的html要用下面的方法处理html文件。


public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
ImageGetter 为处理html中的处理器,生成Drawable对象并返回。


创建ImageGetter 主要实现下面的方法,source为标签中src属性的值。


public Drawable getDrawable(String source)
下例为在TextView和EditView中显示html,并插入图片。


下图只显示html文字,点击按钮会在TextView和EditView文本后添加图片。


public class AndroidTest2Activity extends Activity {
/** Called when the activity is first created. */
TextView tv;
EditText et;
Button addPic;
String ct;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText) this.findViewById(R.id.editText1);

tv=(TextView) this.findViewById(R.id.tv);
ct="aaaaaa";
addPic=(Button) this.findViewById(R.id.AddPic);
addPic.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ct+="";
refreshView();
}

});

refreshView();


}
private void refreshView(){
et.setText(Html.fromHtml(ct,imageGetter,null));
tv.setText(Html.fromHtml(ct,imageGetter,null));
}
ImageGetter imageGetter = new ImageGetter()
{
@Override
public Drawable getDrawable(String source)
{
int id = Integer.parseInt(source);
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d .getIntrinsicHeight());
return d;
}
};


}


1.跳转到浏览器直接访问页面,这段代码是在Activity中拷贝来的,所以有startActivity()方法


Intent intent = new Intent(Intent.ACTION_VIEW, uri);


startActivity(intent);


2.使用TextView显示HTML方法


TextView text1 = (TextView)findViewById(R.id.TextView02);


text1.setText(Html.fromHtml(“网页内容”));


3.直接使用android中自带的显示网页组件WebView


webview = (WebView) findViewById(R.id.WebView01);


webview.getSettings().setjava scriptEnabled(true);


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇实现QQ iPad客户端的对话框平滑移.. 下一篇Android中的Binder机制的简要理解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)