欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-04-13 22:50:11
本文实例为大家分享了android textview图文混合编排的具体代码,供大家参考,具体内容如下 实现技术细节不难,两个要点: 1、html代码的混合编写。...

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。