有时候需要在页面上显示一些html标记的内容,这时候需要使用本节内容:
Html.fromHtml("html内容")
Html.fromHtml("html内容",imgGetter,null ) //显示图片的话
SpannableString 的使用,这个类是
1 显示普通的html内容
2 显示图片内容
3 html内容中超链接标签生效
4 设置文字样式,字体等等。
5 spannable 添加单击事件
package com.example.fragmentdemo1; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Html; import android.text.Html.ImageGetter; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.style.AbsoluteSizeSpan; import android.text.style.BackgroundColorSpan; import android.text.style.BulletSpan; import android.text.style.ForegroundColorSpan; import android.text.style.ImageSpan; import android.text.style.RelativeSizeSpan; import android.text.style.ScaleXSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.SubscriptSpan; import android.text.style.SuperscriptSpan; import android.text.style.TypefaceSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; import android.widget.TextView; /** * html 显示textView 应用 */ public class HtmlActivity extends Activity { TextView mTextView = null; SpannableString msp = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.html); //普通的html显示 showPlainTextView(); //带有图片的html标签使用 showImgTextView(); textStyle(); } //text 显示不同颜色的文本 private void showPlainTextView(){ TextView tv = (TextView) findViewById(R.id.htmlText1); tv.setText(Html.fromHtml( "<b>加粗html:</b> Text with a " + "<a href=\"http://www.google.com\">link</a> " + "created in the Java source code using HTML.")); //使超链接生效 否则超链接不能点击 tv.setMovementMethod(LinkMovementMethod.getInstance());//使超链接生效 } //显示图片 private void showImgTextView(){ //R.drawable.fw 图片的id Spanned text = Html.fromHtml("<img src='"+R.drawable.fw+"'/>",imgGetter,null ); TextView v2 = (TextView) findViewById( R.id.htmlText2); v2.setText( text ); } ImageGetter imgGetter = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = null; // drawable = Drawable.createFromPath(source); // Or fetch it from the URL // Resources resources = getBaseContext().getResources(); //如果这里的图片是 互联网图片 ,需要通过代码下载到本地,然后在引用 //source 是 img标签的src属性里面的内容 drawable = getResources().getDrawable( Integer.parseInt( source)); // Important drawable.getIntrinsicWidth() 宽度和高度可以按照一定比例进行 缩放 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; } }; @SuppressLint("NewApi") public void textStyle( ) { mTextView = (TextView)findViewById(R.id.htmlText3); //创建一个 SpannableString对象 msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合/bot"); //设置字体(default,default-bold,monospace,serif,sans-serif) msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); msp.setSpan(new TypefaceSpan("serif"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置字体大小(绝对值,单位:像素) msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第二个参数boolean dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。 msp.setSpan(new AbsoluteSizeSpan(20,true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍 msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //0.5f表示默认字体大小的一半 msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体大小的两倍 //设置字体前景色 msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置前景色为洋红色 //设置字体背景色 msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置背景色为青色 //设置字体样式正常,粗体,斜体,粗斜体 msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //正常 msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体 msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体 msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗斜体 //设置下划线 msp.setSpan(new UnderlineSpan(), 27, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置删除线 msp.setSpan(new StrikethroughSpan(), 30, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置上下标 msp.setSpan(new SubscriptSpan(), 34, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //下标 msp.setSpan(new SuperscriptSpan(), 36, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //上标 //超级链接(需要添加setMovementMethod方法附加响应) msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //电话 msp.setSpan(new URLSpan("mailto:webmaster@google.com"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //邮件 msp.setSpan(new URLSpan("http://www.baidu.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //网络 msp.setSpan(new URLSpan("sms:4155551212"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //短信 使用sms:或者smsto: msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //彩信 使用mms:或者mmsto: msp.setSpan(new URLSpan("geo:38.899533,-77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //地图 //设置字体大小(相对值,单位:像素) 参数表示为默认字体宽度的多少倍 msp.setSpan(new ScaleXSpan(2.0f), 49, 51, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体宽度的两倍,即X轴方向放大为默认字体的两倍,而高度不变 //设置项目符号 msp.setSpan(new BulletSpan(android.text.style.BulletSpan.STANDARD_GAP_WIDTH,Color.GREEN), 0 ,msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第一个参数表示项目符号占用的宽度,第二个参数为项目符号的颜色 //设置图片 Drawable drawable = getResources().getDrawable(R.drawable.fw); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); msp.setSpan(new ImageSpan(drawable), 53, 57, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mTextView.setText(msp); mTextView.setMovementMethod(LinkMovementMethod.getInstance()); } }
2 布局:
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:gravity="top" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/htmlText1" android:text="html显示" /> <TextView android:id="@+id/htmlText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="#aa0000" android:text="图片显示" /> <TextView android:id="@+id/htmlText3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="1dp"/> </LinearLayout>
3 可以给SpannableString 添加单击事件 ,
附件中是这个功能的例子
public void showMyActivityPage() { //显示MyActivity页面 String text_showMyActivity = "跳转到MyActivity页面......"; SpannableString spannnableString = new SpannableString(text_showMyActivity); //用来拆分字符串 spannnableString.setSpan(new ClickableSpan() { //设置点击时的触发范围为整个字符串 public void onClick(View widget) { Intent intent = new Intent(MainActivity.this,MyActivity.class); //从MainActivity页面跳转到MyActivity页面 startActivity(intent); } }, 0, text_showMyActivity.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView_showMyActivity.setText(spannnableString); textView_showMyActivity.setMovementMethod(LinkMovementMethod.getInstance()); }