Android TextView实现图文混合编排的方法

作者:zhangphil 时间:2022-11-04 23:03:38 

本文实例为大家分享了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实现图文混合编排的方法

来源:http://blog.csdn.net/zhangphil/article/details/53196674

标签:Android,TextView,图文
0
投稿

猜你喜欢

  • 深入浅析Java反射机制

    2023-11-25 07:02:03
  • C#/VB.NET 实现在PDF表格中添加条形码

    2022-12-25 19:58:16
  • 实例讲解Java的Spring框架中的控制反转和依赖注入

    2023-04-24 17:44:49
  • Android学习教程之分类侧滑菜单(5)

    2021-12-26 23:32:37
  • C#生成带注释的dll并引用实现

    2023-08-31 16:01:09
  • C#中分部类和分部方法的应用

    2022-08-16 06:49:05
  • Android编程开发之性能优化技巧总结

    2022-08-26 13:23:00
  • Android应用App更新实例详解

    2023-06-26 21:27:44
  • 解决spring security中遇到的问题

    2023-05-08 11:26:46
  • 关于WPF异步MVVM等待窗体的介绍

    2022-08-03 00:54:19
  • Android图文居中显示控件使用方法详解

    2023-04-04 14:07:24
  • Android中RecyclerView上拉下拉,分割线,多条目的实例代码

    2022-10-14 06:05:07
  • Java使用Collections工具类对List集合进行排序

    2022-10-12 23:59:12
  • Android入门之ActivityGroup+GridView实现Tab分页标签的方法

    2022-11-01 05:38:33
  • Android自定义View模仿即刻点赞数字切换效果实例

    2023-08-26 12:45:39
  • springmvc处理模型数据ModelAndView过程详解

    2022-01-27 05:50:22
  • 深入了解Java File对象的使用

    2022-10-12 00:54:11
  • java实现简单银行家算法

    2022-01-31 23:33:19
  • Unity调取移动端的麦克风进行录音并播放

    2023-06-04 22:18:05
  • Java实现滑动验证码生成(后端工具类)

    2022-02-07 01:26:37
  • asp之家 软件编程 m.aspxhome.com