国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

TextView實現圖文混合編排的方法

2019-10-22 18:30:41
字體:
來源:轉載
供稿:網友

一、簡介

TextView圖文混排,TextView圖文混合編排,TextView圖文混合

在這里實現圖文混合編排使用的是:TextView中預定義的類似Html的標簽

二、方法

* 1、設置好html標簽的文本

String html="<font>圖片1</font><img src='image1'/>";
html+="<font>圖片2</font><img src='image2'/>";
html+="<font>圖片3</font><img src='image3'/>";
html+="<font>圖片4</font><img src='image4'/>";
html+="<font>圖片5</font><img src='image5'/>"; 

* 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標簽

tv_one.setText(Html.fromHtml(text1));

因為有圖片,我們要獲取圖片源,所以上面的那句不行;

所以如下:

CharSequence text=Html.fromHtml(html, new ImageGetter() {中間省略}, null);

new ImageGetter() {中間省略}這部分比較復雜,看實例代碼吧,實質就是取到R文件中圖片對應的ID 

* 3、將CharSequence字符串序列的文本text插入到TextView控件中即可

tv_textAndImage.setText(text);

這里是charSequence是因為Html.fromHtml方法的返回值是Spanned類型,

看下下面的類圖特別好懂:

TextView圖文混排,TextView圖文混合編排,TextView圖文混合

三、代碼實例

效果圖

TextView圖文混排,TextView圖文混合編排,TextView圖文混合

代碼

 fry.ActivityDemo2

 

package fry;import java.lang.reflect.Field;import com.example.textViewDemo1.R;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.Html;import android.text.Html.ImageGetter;import android.widget.TextView;public class ActivityDemo2 extends Activity{  private TextView tv_textAndImage;  @Override  protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.activity02);    setTitle("TextViewDemo2");    tv_textAndImage=(TextView) findViewById(R.id.tv_textAndImage);    //第一步,設置文本    String html="<font>圖片1</font><img src='image1'/>";    html+="<font>圖片2</font><img src='image2'/>";    html+="<font>圖片3</font><img src='image3'/>";    html+="<font>圖片4</font><img src='image4'/>";    html+="<font>圖片5</font><img src='image5'/>";    //第二步,告訴TextView控件這是html,并且獲取文本中的圖片源    CharSequence text=Html.fromHtml(html, new ImageGetter() {            public Drawable getDrawable(String source) {        // TODO Auto-generated method stub        //根據圖片資源ID獲取圖片        //getResources就是去找項目里面的res文件夾        Drawable drawable=getResources().getDrawable(getDrawableResurceID(source));        //一定要加上邊界這部分代碼。要不然drawable會因為信息不完整讀不出來圖片        //分別是left top width height         drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());        return drawable;      }    }, null);    //第三步、將CharSequence字符串序列的文本text插入到TextView控件中即可    tv_textAndImage.setText(text);      }  /**   * 獲取圖片的資源ID   * @param imageName 圖片的名稱   * @return 圖片對應的ID   *    */  private int getDrawableResurceID(String imageName){    //利用反射機制取得圖片的id    /*     * 其實是找com.example.textViewDemo1.R.drawable.image1的值,也就是     * public static final int image1=0x7f020001;     * 也就是0x7f020001     * 例如image1,返回的就是0x7f020001     */    try {      Field field=R.drawable.class.getField(imageName);      return Integer.parseInt(field.get(null).toString());     } catch (Exception e) {      // TODO Auto-generated catch block      e.printStackTrace();    }        return 0;      }}

/textViewDemo1/res/layout/activity02.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <TextView     android:id="@+id/tv_textAndImage"    android:layout_width="match_parent"    android:layout_height="wrap_content"    /></LinearLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文安县| 澄城县| 读书| 晴隆县| 玉树县| 苗栗县| 互助| 龙口市| 大竹县| 石河子市| 翁牛特旗| 康马县| 丰原市| 湛江市| 佛山市| 凤翔县| 荔浦县| 高唐县| 万全县| 抚宁县| 澄江县| 谷城县| 巍山| 北碚区| 佛教| 乐东| 庐江县| 麻栗坡县| 高青县| 隆安县| 瓦房店市| 五莲县| 桃源县| 寿宁县| 保德县| 西城区| 邵武市| 武鸣县| 遂溪县| 蕉岭县| 漠河县|