Android TextView图文混合编排

实现技术细节不难,两个要点:
1、html代码的混合编写。
2,重写ImageGetter。


例如:
布局:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="zhangphil.app.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/text1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/text2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/text3"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:ellipsize="end"  
  24.         android:maxLines="1" />  
  25.   
  26.     <TextView  
  27.         android:id="@+id/text4"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:ellipsize="end"  
  31.         android:maxLines="1" />  
  32. </LinearLayout>  



Java代码:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package zhangphil.app;  
  2.   
  3. import android.graphics.drawable.Drawable;  
  4. import android.support.v4.content.ContextCompat;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.text.Html;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         TextView text1 = (TextView) findViewById(R.id.text1);  
  18.         TextView text2 = (TextView) findViewById(R.id.text2);  
  19.         TextView text3 = (TextView) findViewById(R.id.text3);  
  20.         TextView text4 = (TextView) findViewById(R.id.text4);  
  21.   
  22.         String s = "zhang phil @ csdn Android TextView图文混编";  
  23.   
  24.         CharSequence cs1 = Html.fromHtml(stringMixWithImage1(s), imgageGetter, null);  
  25.         text1.setText(cs1);  
  26.   
  27.         CharSequence cs2 = Html.fromHtml(stringMixWithImage2(s), imgageGetter, null);  
  28.         text2.setText(cs2);  
  29.   
  30.         CharSequence cs3 = Html.fromHtml(stringMixWithImage3(s), imgageGetter, null);  
  31.         text3.setText(cs3);  
  32.   
  33.         CharSequence cs4 = Html.fromHtml(stringMixWithImage4(s), imgageGetter, null);  
  34.         text4.setText(cs4);  
  35.     }  
  36.   
  37.     private String stringMixWithImage1(String string) {  
  38.         return string + "1 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " ";  
  39.     }  
  40.   
  41.     private String stringMixWithImage2(String string) {  
  42.         return "2 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + string;  
  43.     }  
  44.   
  45.     private String stringMixWithImage3(String string) {  
  46.         return string + "3 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " ";  
  47.     }  
  48.   
  49.     private String stringMixWithImage4(String string) {  
  50.         return "4 " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + "<img src='" + R.mipmap.ic_launcher + "'/>" + " " + string;  
  51.     }  
  52.   
  53.     private Html.ImageGetter imgageGetter = new Html.ImageGetter() {  
  54.         @Override  
  55.         public Drawable getDrawable(String source) {  
  56.             int id = Integer.parseInt(source);  
  57.             Drawable d = ContextCompat.getDrawable(getApplicationContext(), id);  
  58.             d.setBounds(00, d.getIntrinsicWidth(), d.getIntrinsicHeight());  
  59.             return d;  
  60.         }  
  61.     };  
  62. }  



代码运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值