本文实例为大家分享了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),null); text2.setText(cs2); CharSequence cs3 = HTML.fromHTML(stringMixWithImage3(s),null); text3.setText(cs3); CharSequence cs4 = HTML.fromHTML(stringMixWithImage4(s),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,d.getIntrinsicWIDth(),d.getIntrinsicHeight()); return d; } };}
代码运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android TextView实现图文混合编排的方法全部内容,希望文章能够帮你解决Android TextView实现图文混合编排的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)