Android TextView图文混合编排

实现技术细节不难,两个要点:
1、html代码的混合编写。
2,重写ImageGetter。

例如:
布局:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="zhangphil.app.MainActivity"> <TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1" /> <TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>

Java代码:

package zhangphil.app;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TextView text1 = (TextView) findViewById(R.id.text1);
TextView text2 = (TextView) findViewById(R.id.text2);
TextView text3 = (TextView) findViewById(R.id.text3);
TextView text4 = (TextView) findViewById(R.id.text4); String s = "zhang phil @ csdn Android TextView图文混编"; CharSequence cs1 = Html.fromHtml(stringMixWithImage1(s), imgageGetter, null);
text1.setText(cs1); CharSequence cs2 = Html.fromHtml(stringMixWithImage2(s), imgageGetter, null);
text2.setText(cs2); CharSequence cs3 = Html.fromHtml(stringMixWithImage3(s), imgageGetter, null);
text3.setText(cs3); CharSequence cs4 = Html.fromHtml(stringMixWithImage4(s), imgageGetter, null);
text4.setText(cs4);
} private String stringMixWithImage1(String string) {
return string + "1 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " ";
} private String stringMixWithImage2(String string) {
return "2 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + string;
} private String stringMixWithImage3(String string) {
return string + "3 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " ";
} private String stringMixWithImage4(String string) {
return "4 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + string;
} private Html.ImageGetter imgageGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable d = ContextCompat.getDrawable(getApplicationContext(), id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
}

代码运行结果:

Android TextView图文混合编排的更多相关文章

  1. TextView实现图文混合编排

    TextView实现图文混合编排 一.简介 在这里实现图文混合编排使用的是:TextView中预定义的类似Html的标签 二.方法 * 1.设置好html标签的文本 String html=" ...

  2. Android TextView中有图片有文字混合排列

    Android TextView中有图片有文字混合排列 1.使用html.fromHtml 2.新建ImageGetter 3.使用<img src>标签 demo: 1.设置文字 ((T ...

  3. Android图文混排-实现EditText图文混合插入上传

    前段时间做了一个Android会议管理系统,项目需求涉及到EditText的图文混排,如图: 在上图的"会议详情"中.须要支持文本和图片的混合插入,下图演示输入的演示样例: 当会议 ...

  4. android TextView EditTextView一些技巧使用 (视图代码布局)

    android TextView 是最常用的控件 可以用作普通的显示,还可以用作有显示文字的按钮,用作有显示图片的图文组合 1. 图文组合 xml 中: <TextView android:id ...

  5. Android TextView 添加下划线的几种方式

    总结起来大概有5种做法:  1. 将要处理的文字写到一个资源文件,如string.xml(使用html用法格式化)   2. 当文字中出现URL.E-mail.电话号码等的时候,可以将TextView ...

  6. Android:TextView 自动滚动(跑马灯) (转)

    Android:TextView 自动滚动(跑马灯)       TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true ...

  7. 使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置

    使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置 http://icaoye.com/virtualbox-run-android/

  8. android Textview动态设置大小

    import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...

  9. Android TextView内容过长加省略号,点击显示全部内容

    在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中:android:ellipsize="end"    省略号在结尾an ...

随机推荐

  1. Unity-Animator深入系列总索引

    花了不少时间完成了这篇Unity Animator学习系列文章,其中大多数内容都来自个人实践,包括API部分很多都是亲测,期望和网上的诸多教程达到互补. 相关参考文档 Unity Animator官方 ...

  2. String和string的区别

    (1)从位置讲 1.string是c#中的的 2.String是 .Net Framework的一个函数名(类),基于using.System的引用 (2)从性质讲 1.string是关键字,Stri ...

  3. [翻译]MapReduce: Simplified Data Processing on Large Clusters

    MapReduce: Simplified Data Processing on Large Clusters MapReduce:面向大型集群的简化数据处理 摘要 MapReduce既是一种编程模型 ...

  4. Vector成员为指针时要注意的问题

    vector的复制是浅复制,所以复制一个包含动态内存的变量的对象的话就会出问题. 解决办法:自己写类的复制构造函数,为新对象的指针开辟新的内存空间. 但当vector离开作用域之后,只会把其成员所占的 ...

  5. 《BI项目笔记》历年理化指标分析Cube的建立

    该系统属于数据仓库系统,与传统的管理信息系统有本质差别,是“面向主题”设计的.“面向主题”的方式,既有利于数据组织和利用,又有利于用户的理解和使用. 分析主题主要维度:烟叶级别.烟叶级别按等级信息.烟 ...

  6. LTE Module User Documentation(翻译12)——X2切换(X2-based handover)

    LTE用户文档 (如有不当的地方,欢迎指正!) 18 X2-based handover   正如 3GPP 定义的,切换是改变用户服务小区的连接方式的过程.这一过程中涉及的两个基站通常称为源基站和目 ...

  7. JSPServlet精华笔记

    一.     JSP (Java Server Pages) JSP是指: ▶    在HTML中嵌入Java脚本代码 ▶    由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码 ▶    ...

  8. Android属性动画完全解析(上),初识属性动画的基本用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系 ...

  9. spring+mongo

    一.程序结构

  10. python中的浅拷贝与赋值不同

    Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2. copy.deep ...