TextView實(shí)現(xiàn)圖文混合編排的方法
一、簡(jiǎn)介
在這里實(shí)現(xiàn)圖文混合編排使用的是:TextView中預(yù)定義的類似Html的標(biāo)簽
二、方法
* 1、設(shè)置好html標(biāo)簽的文本
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標(biāo)簽
tv_one.setText(Html.fromHtml(text1));
因?yàn)橛袌D片,我們要獲取圖片源,所以上面的那句不行;
所以如下:
CharSequence text=Html.fromHtml(html, new ImageGetter() {中間省略}, null);
new ImageGetter() {中間省略}這部分比較復(fù)雜,看實(shí)例代碼吧,實(shí)質(zhì)就是取到R文件中圖片對(duì)應(yīng)的ID
* 3、將CharSequence字符串序列的文本text插入到TextView控件中即可
tv_textAndImage.setText(text);
這里是charSequence是因?yàn)镠tml.fromHtml方法的返回值是Spanned類型,
看下下面的類圖特別好懂:
三、代碼實(shí)例
效果圖
代碼
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); //第一步,設(shè)置文本 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 //根據(jù)圖片資源ID獲取圖片 //getResources就是去找項(xiàng)目里面的res文件夾 Drawable drawable=getResources().getDrawable(getDrawableResurceID(source)); //一定要加上邊界這部分代碼。要不然drawable會(huì)因?yàn)樾畔⒉煌暾x不出來(lái)圖片 //分別是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 圖片對(duì)應(yīng)的ID * */ private int getDrawableResurceID(String imageName){ //利用反射機(jī)制取得圖片的id /* * 其實(shí)是找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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio中生成引用.aar和.jar的方法詳解
這篇文章主要是講解.aar的生成與引用,文中的內(nèi)容屬于完全基礎(chǔ)性概念,對(duì)剛學(xué)習(xí)使用Android studio的朋友們很有幫助,有需要的可以參考學(xué)習(xí),下面來(lái)一起看看吧。2016-09-09Android ViewDragHelper仿淘寶拖動(dòng)加載效果
這篇文章主要為大家詳細(xì)介紹了Android ViewDragHelper仿淘寶拖動(dòng)加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08用Flutter開(kāi)發(fā)自定義Plugin的方法示例
這篇文章主要介紹了用Flutter開(kāi)發(fā)自定義Plugin的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Android編程使用WebView實(shí)現(xiàn)與Javascript交互的方法【相互調(diào)用參數(shù)、傳值】
這篇文章主要介紹了Android編程使用WebView實(shí)現(xiàn)與Javascript交互的方法,可實(shí)現(xiàn)基于WebView與JavaScript相互調(diào)用參數(shù)、傳值的功能,需要的朋友可以參考下2017-03-03Android仿360市場(chǎng)下載按鈕的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于利用Android實(shí)現(xiàn)360市場(chǎng)下載按鈕效果的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),并在文末給出了源碼供大家下載,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-05-05Android集成百度地圖開(kāi)發(fā)流程和注意事項(xiàng)
現(xiàn)在很多項(xiàng)目都需要集成百度地圖,所以就把自己做過(guò)一個(gè)項(xiàng)目的經(jīng)驗(yàn)寫出來(lái)和大家分享,方便自己和大家使用的時(shí)候參考借鑒,下面就來(lái)一起看看Android集成百度地圖開(kāi)發(fā)流程和注意事項(xiàng)吧。2016-09-09仿餓了嗎點(diǎn)餐界面ListView聯(lián)動(dòng)的實(shí)現(xiàn)
這篇文章主要介紹了仿餓了嗎點(diǎn)餐界面ListView聯(lián)動(dòng)的實(shí)現(xiàn)的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Flutter應(yīng)用程序?qū)崿F(xiàn)隱私屏幕示例解析
這篇文章主要為大家介紹了Flutter應(yīng)用程序?qū)崿F(xiàn)隱私屏幕示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09android實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08