ImageSpan的使用

編輯框中加圖片,以前一直以爲很複雜,後來發現android有些類已經很好的實現了這些功能.

代碼如下:

        mSubjectDetailView = (TextView) findViewById(R.id.subject_detail);
        
        CharSequence text = "如圖所示★,dsfdsfdddd,如果fdsfs★東東";
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        String rexgString = "★";
        Pattern pattern = Pattern.compile(rexgString);
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            builder.setSpan(
                    new ImageSpan(this, R.drawable.ic_launcher), matcher.start(), matcher
                            .end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        mSubjectDetailView.setText(builder);
佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/my_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
     <TextView
				android:id="@+id/subject_detail"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"				
				android:minHeight="50dp"
				android:gravity="center_vertical"
				android:text="subject"
				android:textColor="@android:color/primary_text_light_nodisable"
				android:background="@android:color/white"
				android:textSize="25sp" />
</RelativeLayout>

效果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章