TextView實(shí)現(xià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文件中圖片對應(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會因?yàn)樾畔⒉煌暾x不出來圖片
//分別是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 圖片對應(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android SQLite數(shù)據(jù)庫總結(jié)
本文主要介紹了android SQLite數(shù)據(jù)庫的相關(guān)知識。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Android仿微信菜單(Menu)(使用C#和Java分別實(shí)現(xiàn))
這篇文章主要介紹了Android仿微信菜單(Menu)(使用C#和Java分別實(shí)現(xiàn)),本文分別給出C#和Java版的運(yùn)行效果及實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06
Android取消EditText自動獲取默認(rèn)焦點(diǎn)
本文主要介紹了Android取消EditText自動獲取焦點(diǎn)默認(rèn)行為的方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
android開發(fā)教程之開機(jī)啟動服務(wù)service示例
如果開機(jī)啟動一個(gè)Activity,開機(jī)首先看的界面,是你的程序界面,如果為了,開機(jī)后也啟動你的程序,但是不顯示自己程序的界面,就要用Service服務(wù),下面是開機(jī)啟動服務(wù)service示例2014-03-03
Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例,本文用完整的代碼實(shí)例講解了在Android中讀取網(wǎng)絡(luò)中Json數(shù)據(jù)的方法,需要的朋友可以參考下2014-10-10
Android自定義View實(shí)現(xiàn)多片葉子旋轉(zhuǎn)滑動(五)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)多片葉子葉子旋轉(zhuǎn)滑動,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

