1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android文本自动添加图片 Android textView文字添加图片 imageSpan使用

android文本自动添加图片 Android textView文字添加图片 imageSpan使用

时间:2020-03-12 23:11:31

相关推荐

android文本自动添加图片 Android textView文字添加图片 imageSpan使用

如果想为一段文字添加图片,那么可以用到imageSpan来很好的实现

代码:

tvTest = (TextView) findViewById(R.id.test);

CharSequence text = "效果测试★,效果测试,效果★测试";

SpannableStringBuilder builder = new SpannableStringBuilder(text);

String rexgString = "★";

Pattern pattern = pile(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);

}

tvTest.setText(builder);

效果图:

如果需要多段文字用图片进行分辨,可以用StringBuilder添加特殊字符进行区分.

如果知道需要替换图片的位置,也可以用这种方法:

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

ImageSpan imgSpan = new ImageSpan(this, b);

SpannableString spanString = new SpannableString("icon");

spanString.setSpan(imgSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

textView2.setText(spanString);

textView2.append("测试效果测试效果测试效果");

效果图:

这种是替换某个文字的方法.如果不需要替换,可以在

SpannableString spanString = new SpannableString("icon");

直接放入全部文字,然后确定需要放入图片的位置即可

SpannableString还有很多其他用法,可以设置指定文字大小,颜色,下划线等效果

1、BackgroundColorSpan 背景色2、ClickableSpan 文本可点击,有点击事件 3、ForegroundColorSpan 文本颜色(前景色) 4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter) 5、MetricAffectingSpan 父类,一般不用 6、RasterizerSpan 光栅效果 7、StrikethroughSpan 删除线(中划线) 8、SuggestionSpan 相当于占位符 9、UnderlineSpan 下划线 10、AbsoluteSizeSpan 绝对大小(文本字体) 11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。 12、ImageSpan 图片 13、RelativeSizeSpan 相对大小(文本字体) 14、ReplacementSpan 父类,一般不用 15、ScaleXSpan 基于x轴缩放 16、StyleSpan 字体样式:粗体、斜体等 17、SubscriptSpan 下标(数学公式会用到) 18、SuperscriptSpan 上标(数学公式会用到) 19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色) 20、TypefaceSpan 文本字体

21、URLSpan 文本超链接

大家需要什么需求了可以继续查询使用方法

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。