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. linux定时执行任务

    (1)Linux下如何定时执行php脚本?(2)Linux下如何设置定时任务?(3)Crontab定时执行程序 核心提示:键入 crontab -e 编辑crontab服务文件 分为两种情况:(还有一 ...

  2. Maven学习(三) -- 仓库

    标签(空格分隔): 学习笔记 坐标和依赖时任何一个构件在Maven世界中的逻辑表示方式:而构件的物理表示方式是文件,Maven通过仓库来同意管理这些文件. 任何一个构件都有其唯一的坐标,根据这个坐标可 ...

  3. BZOJ 3229: [Sdoi2008]石子合并

    3229: [Sdoi2008]石子合并 时间限制: 3 Sec  内存限制: 128 MB提交: 497  解决: 240[提交][][] 题目描述 在一个操场上摆放着一排N堆石子.现要将石子有次序 ...

  4. 利用gitHub搭建博客

    ##1.gitHub Page的的使用我觉得这边博文写的很清楚,方法.步骤.优缺点以及实例,所以就借用一下啦^_^ [搭建一个免费的,无限流量的Blog](http://www.ruanyifeng. ...

  5. Trick

    1. var b = a.slice(beginIndex,endIndex); [].slice.call( [] ) Array.prototype.slice.call([]) will cop ...

  6. 监控 Linux Unix Solaris AIX, swap page in / swap page out

    vmstat 的 pi/po si/so --监控一天 vmstat 5 17280> vmstat.txt sar -W 1.得到数据 (linux 的 /var/log/sar/saX 自带 ...

  7. html 通用 遮罩弹出层 弹出后 支持跳转页面

    //showMessage 提示的内容默认为空必填 buttonText:按钮显示的内容默认为"确定" 传入 "" 为默认 url:跳转链接 传入"& ...

  8. 【bzoj1798】维护序列

    线段树维护两个标记. *0的操作在实质上没有任何影响. #include <cstdio> #include <cctype> #define rep(i,a,b) for ( ...

  9. js输入,输出基本操作

  10. 高版本正方教务系统上传后缀过滤不严导致能直接上传Webshell

    在旧版本中有一个利用插件上传文件的漏洞,但是在新版本中已经没有了这个插件.这个漏洞是由于过滤不严造成的,可以直接上传Webshell进行提权,由于代码在DLL中,全国大部分高校均有此漏洞,影响范围很大 ...