您的位置:首页 > 其它

TextView和EditText中添加图片(ImageSpan)

2013-08-14 16:18 519 查看
编辑框中加图片,以前一直以为很复杂,后来发现android有些类已经很好的实现了这些功能.

代码如下:

[java] view
plaincopy

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);

布局:

[html] view
plaincopy

<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>

效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: