Android TextView实现图文混合编排的方法
发布时间 - 2026-01-11 02:18:12 点击率:次本文实例为大家分享了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图文混合编排
# TextView图文混排
# TextView图文编排
# Android中TextView和ImageView实现倾斜效果
# Android TextView实现带链接文字事件监听的三种常用方式示例
# Android用户输入自动提示控件AutoCompleteTextView使用方法
# Android TextView对齐的两种方法
# Android实现带动画效果的可点击展开TextView
# Android TextView中部分文字高亮显示
# Android自定义textview实现竖直滚动跑马灯效果
# Android中TextView显示圆圈背景或设置圆角的方法
# Android 自定义TextView去除paddingTop和paddingBottom
# 重写
# 大家分享
# 具体内容
# 大家多多
# id
# MainActivity
# app
# wrap_content
# zhangphil
# layout_height
# match_parent
# layout_width
# context
# vertical
# orientation
# drawable
# graphics
# import
# support
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
iOS中将个别页面强制横屏其他页面竖屏
JavaScript如何操作视频_媒体API怎么控制播放
如何获取免费开源的自助建站系统源码?
laravel怎么用DB facade执行原生SQL查询_laravel DB facade原生SQL执行方法
如何在云主机上快速搭建网站?
Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程
如何在HTML表单中获取用户输入并用JavaScript动态控制复利计算循环
Laravel如何处理异常和错误?(Handler示例)
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
Laravel Debugbar怎么安装_Laravel调试工具栏配置指南
Laravel如何使用Eloquent ORM进行数据库操作?(CRUD示例)
利用vue写todolist单页应用
海南网站制作公司有哪些,海口网是哪家的?
C++用Dijkstra(迪杰斯特拉)算法求最短路径
历史网站制作软件,华为如何找回被删除的网站?
Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
高防服务器租用首荐平台,企业级优惠套餐快速部署
Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道
,在苏州找工作,上哪个网站比较好?
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
Laravel策略(Policy)如何控制权限_Laravel Gates与Policies实现用户授权
企业网站制作这些问题要关注
html5源代码发行怎么设置权限_访问权限控制方法与实践【指南】
网站制作企业,网站的banner和导航栏是指什么?
JavaScript中的标签模板是什么_它如何扩展字符串功能
Angular 表单中正确绑定输入值以确保提交与验证正常工作
深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?
如何快速搭建安全的FTP站点?
如何在 Go 中优雅地映射具有动态字段的 JSON 对象到结构体
HTML透明颜色代码在Angular里怎么设置_Angular透明颜色使用指南【详解】
Laravel distinct去重查询_Laravel Eloquent去重方法
详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
ChatGPT回答中断怎么办 引导AI继续输出完整内容的方法
Laravel怎么调用外部API_Laravel Http Client客户端使用
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
香港服务器网站卡顿?如何解决网络延迟与负载问题?
利用 Google AI 进行 YouTube 视频 SEO 描述优化
通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】
免费网站制作appp,免费制作app哪个平台好?
如何在宝塔面板中修改默认建站目录?
Laravel PHP版本要求一览_Laravel各版本环境要求对照
Laravel如何实现密码重置功能_Laravel密码找回与重置流程
Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】
Laravel如何为API生成Swagger或OpenAPI文档
nginx修改上传文件大小限制的方法
手机软键盘弹出时影响布局的解决方法
微信小程序 五星评分(包括半颗星评分)实例代码
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
如何在七牛云存储上搭建网站并设置自定义域名?

