要在 Android 的文字元件裡做出「紅字、藍色斜體、加下劃線,中間還能插入圖片」這種效果,不必自己拼一堆 Span。系統提供了兩條路:在版面檔用屬性設定,或在程式碼裡用 HTML 與 ImageSpan 動態組裝。
<EditText
android:id="@+id/editText1"
android:textColor="#2BD54D"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
Html.fromHtml() 支援 <font color>、<i>、<u> 等常用標籤,把它產生的 Spanned 直接塞給 TextView 即可,寫法比手動 new 一堆 Span 直觀得多。
editText2.setText(Html.fromHtml(
"<font color=#E61A6B>紅色代碼</font> "
+ "<i><font color=#1111EE>藍色斜體代碼</font></i>"
+ "<u><i><font color=#1111EE>藍色斜體加粗體下劃線代碼</font></i></u>"));
圖片沒辦法用 HTML 直接搞定,得靠 ImageSpan。流程是:把 Drawable 的邊界用 setBounds 設定好(這步最常被忘記,忘了圖片就不會顯示),再用 SpannableString 把 ImageSpan 套到一段文字上,最後設定給 TextView。
package rw.textView;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class TextViewTestActivity extends Activity {
private EditText editText, editText2, editText3;
private ImageButton imageButton01, imageButton02, imageButton03;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
imageButton01 = (ImageButton) findViewById(R.id.imageButton1);
imageButton02 = (ImageButton) findViewById(R.id.imageButton2);
imageButton03 = (ImageButton) findViewById(R.id.imageButton3);
editText2.setText(Html.fromHtml(
"<font color=#E61A6B>紅色代碼</font> "
+ "<i><font color=#1111EE>藍色斜體代碼</font></i>"
+ "<u><i><font color=#1111EE>藍色斜體加粗體下劃線代碼</font></i></u>"));
imageButton01.setOnClickListener(new MyListener());
imageButton02.setOnClickListener(new MyListener());
imageButton03.setOnClickListener(new MyListener());
}
class MyListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1: SetImage(R.drawable.amazed); break;
case R.id.imageButton2: SetImage(R.drawable.angry); break;
case R.id.imageButton3: SetImage(R.drawable.isync); break;
default: break;
}
}
}
void SetImage(int dra) {
Drawable drawable = getResources().getDrawable(dra);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
SpannableString spannableString = new SpannableString("pics");
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
spannableString.setSpan(imageSpan, 0, spannableString.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText3.setText(spannableString);
}
}
三個按鈕各自對應一個表情圖,點下去就換掉 editText3 裡的圖,這其實就是最樸素的「表情插入」功能。若要更進一步做到「文字與圖片交錯」,可以呼叫 Html.fromHtml 時傳入自訂的 ImageGetter,在 getDrawable 裡依 src 名稱回傳對應的 Drawable,就能用 <img src='...'> 直接在 HTML 字串中放圖。另外要提醒的是,ALIGN_BASELINE 會讓圖片底部對齊文字基線,想讓圖片與文字垂直居中則改用 ALIGN_BASELINE 以外的對齊方式並自行微調 setBounds 的上下偏移。