首先我们看看代码:
1.activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.himi.textview_img.MainActivity" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/list_item_divide_operate" /> <TextView android:id="@+id/textview01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/list_item_divide_operate" /> <TextView android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>
2.然后是MainActivity.java:
package com.himi.textview_img; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Html; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private TextView tv1; private TextView tv2; private final String sText1 = "测试图片信息:<img src=\""+R.drawable.ic_launcher+"\" />"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.textview); tv1 = (TextView)findViewById(R.id.textview01); tv2 = (TextView)findViewById(R.id.textview02); //利用TextView直接显示Html文字内容 tv.setText( Html.fromHtml( "<h1>任逍遥</h1>"+ "<a href=http://localhost/http/test.php>link</a>"+ "<p>我和你</p>" )); //利用textview.setText(Html.fromHtml(……,……,……));方法显示Html文件中img图片,资源文件图片 tv1.setText(Html.fromHtml(sText1,new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = null; int rid = Integer.parseInt(source); drawable = getResources().getDrawable(rid); drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); return drawable; } },null)); tv2.setText("文字"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
这里重点提一下:Html类和它的静态方法fromHtml()
fromHtml()是重载的,有两种格式如下:
(1)public static Spanned fromHtml(String source)
通过返回的Spanned(继承自CharSequence):表示可显示的样式文本
(2)public static Spanned fromHtml(String source,Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
source :包含Html的字符串。
imageGetter :imageGetter对象。当系统解析到 <img> 标签时就会调用imageGetter对象的getDrawable方法,并 将 src 属性值传递传入 getDrawable 方法。 至于 src 属性的具体含义,就要在getDrawable方法中确定了。getDrawable方法返回一个Drawable对象。我们可以从res/drawable资源、assets资源、SD卡以及网络上获得图像资源,并分装成Drawable对象。
tagHandler :TagHandler对象。系统每处理一个标签时都会调用该对象的handleTag方法。不过这个参数并不常用。如果不用可以设为null。
上面提到是项目资源文件利用Html这个类在TextView中显示,当然类似的还是网络图片,手机本地图片在利用Html这个类在TextView显示代码示例如下:
(1)网络图片:
final String sText = "测试图片信息:<br><img src=\"http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg\" />"; tView.setText(Html.fromHtml(sText, imageGetter, null)); final Html.ImageGetter imageGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable=null; URL url; try { url = new URL(source); drawable = Drawable.createFromStream(url.openStream(), ""); } catch (Exception e) { e.printStackTrace(); return null; } drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; }; }
(2)手机本地图片:
final String sText2 = "测试图片信息:<img src=\"/mnt/sdcard/temp/1.jpg\" />"; tView.setText(Html.fromHtml(sText2, imageGetter, null)); final Html.ImageGetter imageGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable=null; drawable=Drawable.createFromPath(source); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; }; }
特别注意三种不同图片的路径:
(1)资源文件:<img src=\""+R.drawable.ic_launcher+"\" />
(2)本地文件:<img src=\"/mnt/sdcard/temp/1.jpg\" />
(3)网络文件:<img src=\"http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg\" />