一、怎么在TextView中设置首行缩进两个字符

在string资源文件中,在文字的前面加入”\u3000\u3000”即可实现首行缩进

在Java代码中,使用setText("\u3000\u3000"+xxxxx);

二、TextView中的图文混排和不同颜色、大小字体的显示

方法一:设置不同颜色、大小、图文混排的效果通过SpannableString,并且设置各种Span实现的。

SpannableString的setSpan方法需要几个参数:

public void setSpan (Object what, int start, int end, int flags)

what传入各种Span类型的实例,start和end标记要替代的文字内容的范围,flags是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果,可以传入Spanned.SPAN_EXCLUSIVE_EXCLUSIVE、Spanned.SPAN_INCLUSIVE_EXCLUSIVE、Spanned.SPAN_EXCLUSIVE_INCLUSIVE、Spanned.SPAN_INCLUSIVE_INCLUSIVE几个参数,INCLUSIVE表示应用该效果,EXCLUSIVE表示不应用该效果,如Spanned.SPAN_INCLUSIVE_EXCLUSIVE表示对前面的文字应用该效果,而对后面的文字不应用该效果。

可以使用的主要几种Span类型为:

ImageSpan 可以使用图片替换文字达到图文混排的效果,例如在一般聊天工具当中在文字和表情一起发的状态。

使用方法为:

Drawabledrawable=mContext.getResources().getDrawable(R.drawable.new_topic_drawable);drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);spanString.setSpan(imageSpan,spanString.length()-1,spanString.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);

ForegroundColorSpan 设置文字前景色,即文字本身的颜色

spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#f74224")), 0,titleText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

AbsoluteSizeSpan 设置文字的绝对大小值

spanString.setSpan(new AbsoluteSizeSpan(11),0,spanString.length(),titleText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

UrlSpan 设置超链接

URLSpan span = new URLSpan("tel:0123456789");spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

BackgroundColorSpan 设置文字背景色

BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

StyleSpan 字体设置

StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);spanString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Typeface中有四个Style常量,分别是BOLD粗体、ITALIC斜体、BOLD_ITALIC粗斜体、NORMAL正常

StrikethroughSpan 删除线

StrikethroughSpan span = new StrikethroughSpan();spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

UnderlineSpan下划线

UnderlineSpan span = new UnderlineSpan();spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

在具体实践中可以对同一范围内的文字叠加不同的span,如文字的大小和文字的颜色可以叠加使用,可以组合出不同的效果

此外在API 17中,TextView自带了几个方法也可以达到图文混排的效果:

public void setCompoundDrawablesRelative (Drawable start, Drawable top, Drawable end, Drawable bottom)

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.Related XML Attributes
android:drawableStart
android:drawableTop
android:drawableEnd
android:drawableBottompublic void setCompoundDrawablesRelativeWithIntrinsicBounds (Drawable start, Drawable top, Drawable end, Drawable bottom)

Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

Related XML Attributes
android:drawableStart
android:drawableTop
android:drawableEnd
android:drawableBottompublic void setCompoundDrawablesRelativeWithIntrinsicBounds (int start, int top, int end, int bottom)
Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

Related XML Attributes
android:drawableStart
android:drawableTop
android:drawableEnd
android:drawableBottom
Parameters
start    Resource identifier of the start Drawable.
top    Resource identifier of the top Drawable.
end    Resource identifier of the end Drawable.
bottom    Resource identifier of the bottom Drawable.

由于项目中要达到兼容2.3.x版本的目的,并未使用,读者也可以自行研究以下相关方法的使用

转载于:https://www.cnblogs.com/txlbupt/p/3640826.html

Android中实现不同文字颜色和图文混排的Span总结相关推荐

  1. 安卓直接展示html,Android textView展示html图片,实现图文混排,点击查看大图片

    Android textView展示html图片,实现图文混排,点击查看大图片 最近要展示html在textView上,实现图文混排,并且图片可以点击放大,所以去研究了一下,效果图如下: 我们知道te ...

  2. textView 的设置文本中某一文字的字体颜色以及图文混排

    ios7 新增的TextKit 技术 从书上看到的-- 首先需要实例化3个对象 NSTextStorage //存储字符的相关属性 包括颜色等 NSLayoutManager //将字符变化反应到Ns ...

  3. Android:仿小米便签,图文混排

    经过两天的摸索,终于实现,现在贴出来,供自己学习记录及与小伙伴们交流分享 先看下效果图: (添加按钮,用于添加图片) 话不多说,小翠上代码: 1.布局文件: <RelativeLayout xm ...

  4. 图文混排时,图片和文字垂直如何居中

    图文混排时,图片和文字垂直如何居中 首先我们来说一说line-height和vertical:middle的区别 line-height:行高,可以使标签内部一行的内容进行垂直对齐 vertical: ...

  5. 【小松教你手游开发】【系统模块开发】图文混排 (在label中插入表情)

    本身ngui是自带图文混排的,这个可以在ngui的Example里找到.但是为什么不能用网上已经说得很清楚,比如雨松momo的http://www.xuanyusong.com/archives/29 ...

  6. NGUI-制作位图字体以及图文混排

    制作字体过程 首先得下载一个位图制作工具Bitmap font generator,可以点击这里下载 1.新建txt文件,输入字体里面包含的文字 2.保存为utf-8格式:点击文件另存为,选择编码格式 ...

  7. 03-CSS操作--CSS操作规范(书写技巧)+通栏布局+标签的注意事项+三大类标签+边距合并问题+图文混排+图像签名+水平居中+垂直居中

    一.CSS操作规范 1.CSS书写技巧 (1)最外层是一个<div id="bigDiv">,将所有元素都包进去 (2)去除<body>的外边距(body{ ...

  8. android textview 添加图片大小,Android_Android中使用TextView实现图文混排的方法,向TextView或EditText中添加图像比 - phpStudy...

    Android中使用TextView实现图文混排的方法 向TextView或EditText中添加图像比直接添加文本复杂一点点,需要用到标签. 只有一个src属性,该属性原则上应该指向一个图像地址或可 ...

  9. Android中设置TextView的颜色setTextColor

    tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...

最新文章

  1. CSS中连接属性的排序
  2. validator的Symbol Coverage没有100%
  3. vue中搜索关键词,使文本标红
  4. sift+图像匹配 算法
  5. 深入Spring Boot:快速集成Dubbo + Hystrix
  6. PHP的session阻塞问题
  7. 出路在哪里?出路在于思路!智者无敌
  8. Tip of the day的bug
  9. VAV系统类毕业论文文献都有哪些?
  10. YOLOv4 绘制PR曲线
  11. 自己做的SIP软电话
  12. esp32 cam 内网穿透 视频传输
  13. markdown文档:一个简单标记语言的使用及GitHub实际应用
  14. Windows下装ubuntu系统
  15. 7-20 打印九九口诀表 (15 分) 下面是一个完整的下三角九九口诀表:
  16. 使用recycleview 实现viewpager 功能,并带有指示器。(仿高德交通路线规划实现)
  17. html samp如何转成块,HTML code|kbd|samp|var程序标签应用说明-立地货
  18. 共享网络打印机无法连接计算机,共享打印机连不上的解决方法-电脑自学网
  19. html版心宽度怎么设置,Word中版心尺寸是什么意思?怎样设置版心尺寸?
  20. 工商管理专业知识与实务(初级)【8】

热门文章

  1. Flutter Web 应用程序的 URL 中删除前导`#`
  2. dispatch作用 react_「React系列」手把手带你撸后台系统(Redux与路由鉴权)
  3. javaweb k8s_阿里云部署K8Sweb项目
  4. 视频百教程度云_腾讯视频的进击
  5. android布局加色,android – 如何以编程方式将LinearLayout添加背景颜色,并将权重添加到另一个布局...
  6. 校宝在线携手神策数据 数据赋能产品服务体验双升级
  7. 8/人天,小记一次 JAVA(APP后台) 项目改造 .NET 过程(后台代码已完整开源于 Github)...
  8. 关于Webstrom连接github的配置问题。
  9. Linux下统计局域网流量
  10. php+mysql+pdo连接数据库