32Spannable的(de)使用(Android顯示html帶圖片(表情開發)
發表時(shí)間:2020-11-6
發布人(rén):融晨科技
浏覽次數:73
Android中顯示html文件要(yào / yāo)用Html.fromHtml(...)處理過的(de)返回值,返回值可以(yǐ)成爲(wéi / wèi)setText()的(de)參數。隻顯示帶文本的(de)html可以(yǐ)用下面的(de)方法處理html文件。
public static Spanned fromHtml (String source)
顯示帶圖片的(de)html要(yào / yāo)用下面的(de)方法處理html文件。
public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)<span style="font-family: Simsun;font-size:14px; text-indent: 2em; background-color: rgb(255, 255, 255);">ImageGetter 爲(wéi / wèi)處理html中<img>的(de)處理器,生成Drawable對象并返回。</span>
創建ImageGetter 主要(yào / yāo)實現下面的(de)方法,source爲(wéi / wèi)<img>标簽中src屬性的(de)值。
public Drawable getDrawable(String source)
下例爲(wéi / wèi)在(zài)TextView和(hé / huò)EditView中顯示html,并插入圖片。
下圖隻顯示html文字,點擊按鈕會在(zài)TextView和(hé / huò)EditView文本後添加圖片。
[img]http://img.blog.csdn.net/20141231084749890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZ1ZHVvX2xvdmVpdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
代碼:
package com.example.test; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Html; import android.text.Html.ImageGetter; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity 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.activity_main); et=(EditText) this.findViewById(R.id.editText1); tv=(TextView) this.findViewById(R.id.tv); ct="aaa<font color=\"red\">aaa</font>"; addPic=(Button) this.findViewById(R.id.AddPic); addPic.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub ct+="<img src=http://www.sjsjw.com/""+R.drawable.ic_launcher+"/"/>"; 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; } }; }
<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" tools:context="${relativePackage}.${activityClass}" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/AddPic" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>