知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們在追求其視覺表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
32Spannable的使用(Android顯示html帶圖片(表情開發(fā))
發(fā)表時(shí)間:2020-11-6
發(fā)布人:葵宇科技
瀏覽次數(shù):73
Android中顯示html文件要用Html.fromHtml(...)處理過的返回值,返回值可以成為setText()的參數(shù)。只顯示帶文本的html可以用下面的方法處理html文件。
public static Spanned fromHtml (String source)
顯示帶圖片的html要用下面的方法處理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 為處理html中<img>的處理器,生成Drawable對象并返回。</span>
創(chuàng)建ImageGetter 主要實(shí)現(xiàn)下面的方法,source為<img>標(biāo)簽中src屬性的值。
public Drawable getDrawable(String source)
下例為在TextView和EditView中顯示html,并插入圖片。
下圖只顯示html文字,點(diǎn)擊按鈕會(huì)在TextView和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>
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站開發(fā)
- 楚雄網(wǎng)站建設(shè)公司
- 網(wǎng)站建設(shè)優(yōu)化
- 政府網(wǎng)站建設(shè)服務(wù)
- 云南小程序被騙蔣軍
- 云南科技公司
- 河南小程序制作
- 江蘇小程序開發(fā)
- 云南網(wǎng)站建設(shè)開發(fā)
- .net網(wǎng)站
- 網(wǎng)站建設(shè)
- 開發(fā)制作小程序
- 關(guān)鍵詞快速排名
- 報(bào)廢車拆解軟件
- 云南網(wǎng)站設(shè)計(jì)
- 汽車拆解管理軟件
- 云南網(wǎng)站建設(shè)招商
- 開發(fā)微信小程序
- 小程序模板開發(fā)公司
- 報(bào)廢車
- 大理網(wǎng)站建設(shè)公司
- 云南小程序開發(fā)
- 支付寶小程序被騙
- 云南小程序被騙
- 出入小程序
- 汽車報(bào)廢系統(tǒng)
- 汽車報(bào)廢回收
- 云南網(wǎng)站優(yōu)化公司
- 網(wǎng)站建設(shè)方案 doc
- 汽車報(bào)廢管理系統(tǒng)