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>
效果:
代码如下:
[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>
效果:
相关文章推荐
- TextView和EditText中添加图片(ImageSpan)
- Android textView文字添加图片 imageSpan使用
- Android笔记:TextView和编辑框中添加图片,ImageSpan,SpannableStringBuilder用法
- android平台TextView使用ImageSpan展示GIF图片
- 利用Span实现TextView/EditText的灵活表现--改变字体外观,添加点击,超链接
- 利用SpannableString和ImageSpan在textview中插入图片的方法
- 在TextView/EditText中添加图片方法
- 在TextView或者EditView中添加图片
- 为Textview里面的ImageSpan添加点击响应事件
- 用SpannableString和ImageSpan在textview中插入图片
- Android--EditText/TextView 中添加图片,以及点击图片.
- 带行间距的TextView,让文字和ImageSpan中的图片居中显示
- Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解
- 自定义可点击的ImageSpan并在TextView中内置“View“
- iOS为imageView添加图片实现动画
- TextView 多行文字末尾添加图片
- TextView控件上添加表情图片
- Android - VGA,ADB,DDMS,安卓开发环境目录结构,安卓目录结构,常见布局,点击事件的几种写法,控件(Button/TextView/ImageView/EditText)
- Android TextView中添加图片的方法
- android TextView、EditText对部分内容设置颜色、字体、超链接、图片