AndroidTextView實現圖文混合編排的方法

本文實例為大家分享了Android TextView圖文混合編排的具體代碼,供大家參考,具體內容如下

我們提供的服務有:成都做網站、網站制作、成都外貿網站建設、微信公眾號開發(fā)、網站優(yōu)化、網站認證、清水河ssl等。為近千家企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的清水河網站制作公司

實現技術細節(jié)不難,兩個要點:

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實現圖文混合編排的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯。

本文名稱:AndroidTextView實現圖文混合編排的方法
網頁地址:http://www.muchs.cn/article48/ghgeep.html

成都網站建設公司_創(chuàng)新互聯,為您提供企業(yè)網站制作App開發(fā)、網站營銷、品牌網站建設、、微信小程序

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都seo排名網站優(yōu)化