TextView實現圖文混合編排,textview圖文編排

來源:互聯網
上載者:User

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類型,

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

 

 

三、代碼執行個體

代碼

 fry.ActivityDemo2

 1 package fry; 2  3 import java.lang.reflect.Field; 4  5 import com.example.textViewDemo1.R; 6  7 import android.app.Activity; 8 import android.graphics.drawable.Drawable; 9 import android.os.Bundle;10 import android.text.Html;11 import android.text.Html.ImageGetter;12 import android.widget.TextView;13 14 public class ActivityDemo2 extends Activity{15     private TextView tv_textAndImage;16     @Override17     protected void onCreate(Bundle savedInstanceState) {18         // TODO Auto-generated method stub19         super.onCreate(savedInstanceState);20         setContentView(R.layout.activity02);21         setTitle("TextViewDemo2");22         tv_textAndImage=(TextView) findViewById(R.id.tv_textAndImage);23         //第一步,設定文本24         String html="<font>圖片1</font><img src='image1'/>";25         html+="<font>圖片2</font><img src='image2'/>";26         html+="<font>圖片3</font><img src='image3'/>";27         html+="<font>圖片4</font><img src='image4'/>";28         html+="<font>圖片5</font><img src='image5'/>";29         //第二步,告訴TextView控制項這是html,並且擷取文本中的圖片源30         CharSequence text=Html.fromHtml(html, new ImageGetter() {31             32             public Drawable getDrawable(String source) {33                 // TODO Auto-generated method stub34                 //根據圖片資源ID擷取圖片35                 //getResources就是去找項目裡面的res檔案夾36                 Drawable drawable=getResources().getDrawable(getDrawableResurceID(source));37                 //一定要加上邊界這部分代碼。要不然drawable會因為資訊不完整讀不出來圖片38                 //分別是left top width height 39                 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());40                 return drawable;41             }42         }, null);43         //第三步、將CharSequence字串序列的文本text插入到TextView控制項中即可44         tv_textAndImage.setText(text);45         46     }47     /**48      * 擷取圖片的資源ID49      * @param imageName 圖片的名稱50      * @return 圖片對應的ID51      * 52      */53     private int getDrawableResurceID(String imageName){54         //利用反射機製取得圖片的id55         /*56          * 其實是找com.example.textViewDemo1.R.drawable.image1的值,也就是57          * public static final int image1=0x7f020001;58          * 也就是0x7f02000159          * 例如image1,返回的就是0x7f02000160          */61         try {62             Field field=R.drawable.class.getField(imageName);63             return Integer.parseInt(field.get(null).toString());  64         } catch (Exception e) {65             // TODO Auto-generated catch block66             e.printStackTrace();67         }68         69         return 0;70         71     }72 }

 

/textViewDemo1/res/layout/activity02.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6     <TextView  7         android:id="@+id/tv_textAndImage" 8         android:layout_width="match_parent" 9         android:layout_height="wrap_content"10         />11 12 </LinearLayout>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.