linux中html的图片显示不出来的,Android中显示html标签或者带图片

Android中显示html文件要用Html.fromHtml(...)处理过的返回值,返回值可以成为setText()的参数。

只显示带文本的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文本后添加图片。

6162ff354834c9e317c7ba462f64c4b0.png

aa8b0cdf8b7421e42dba5896c1dd097f.png

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+="%5C%22%22+R.drawable.icon+%22%5C%22";

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()方法

Uri uri = Uri.parse("http://www.linuxidc.com"); //要链接的地址

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().setJavaScriptEnabled(true);

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值