Android项目实战(十四):TextView显示html样式的文字
项目需求:
TextView显示一段文字,格式为:白雪公主(姓名,字数不确定)向您发来了2(消息个数,不确定)条消息
这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色。
一开始我想的是用(转) SpannableString与SpannableStringBuilder来实现,因为它可以实现一段文字显示不同的颜色
但是貌似它只能固定哪些位置的文字显示什么样式,于是乎放弃。
然后就想到了用
Html.fromHtml(String str)
来实现。
看方法名很简单,就是可以显示字符串str对应的html格式的文本
比如:
Html.fromHtml(<font color='red' size='24'>你好</font>" )
就将你好以html格式显示了,红色字体 大小24
那么通过一个小Demo看下这个方法的简单使用:
我有三个字符串,字符串中姓名、数字长度都是不同的,实现让姓名显示红色,数字显示蓝色,其他文字显示默认灰色的效果
先写布局文件,三个TextView
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/html_text" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/html_text2" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/html_text3" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然后Activity 的onCreate()方法
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.html_text); textView2 = (TextView) findViewById(R.id.html_text2); textView3 = (TextView) findViewById(R.id.html_text3); names = new ArrayList<>(); counts = new ArrayList<>(); message = new ArrayList<>(); names.add("奥特曼"); names.add("白雪公主与七个小矮人"); names.add("沃德天·沃纳陌帅·帅德·布耀布耀德 "); counts.add(1); counts.add(123); counts.add(9090909); for (int i = 0; i < 3; i++) { message.add("<font color='red' size='20'>"+names.get(i)+"</font>"+"向您发来"+ "<font color='blue' size='30'>"+counts.get(i)+"</font>"+"条信息"); } textView.setText(Html.fromHtml(message.get(0))); textView2.setText(Html.fromHtml(message.get(1))); textView3.setText(Html.fromHtml(message.get(2))); }
看下效果图,是不是很简单,只要简单的会html 就可实现这种效果
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android项目实战(十二):解决OOM的一种偷懒又有效的办法
原文: Android项目实战(十二):解决OOM的一种偷懒又有效的办法 在程序的manifest文件的application节点加入android:largeHeap=“true”即可。 对,只需要一句话! 那么这行代码的意思是什么呢? 简单的说就是使该APP获取最大可分配的内存,以便解决OOM问题、 但是、OOM问题出现的原因总得来说有两点: 1、某个手机的内存真的很少 2、代码问题,比如没有处理好Bitmap图片的大小 可以说,出现OOM的情况基本都是第二种情况,那么就需要修改代码,看看哪里没有处理好。 所以说给应用程序使用:android:largeHeap=“true” 这个方法解决OOM的问题只是一种治标不治本的方式 所以,对于本身对内存要求过大的图片或者视频应用,我们可以使用largeHeap。 有人说使用这行代码会对GC产生影响,也有人说不会。这里博主就确定不了了 ,大家可以上网查看下。 -------------------------------------------------------------------------------------- 最后加...
- 下一篇
Android项目实战(十五):自定义不可滑动的ListView和GridView
原文: Android项目实战(十五):自定义不可滑动的ListView和GridView 不可滑动的ListView (RecyclweView类似) public class NoScrollListView extends ListView { public NoScrollListView(Context context, AttributeSet attrs) { super(context,attrs); } public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, mExpandSpec); } } 用于情况: 社交,社区APP ,例如 QQ空间,一条说说,说说下面的评论用不可滑动的ListView来展示 --------------...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Hadoop3单机部署,实现最简伪集群
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装