溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android TextView實(shí)現(xiàn)圖文混合編排的方法

發(fā)布時(shí)間:2020-09-21 02:33:54 來源:腳本之家 閱讀:154 作者:zhangphil 欄目:移動(dòng)開發(fā)

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

實(shí)現(xiàn)技術(shù)細(xì)節(jié)不難,兩個(gè)要點(diǎn):

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;
 }
 };
}

代碼運(yùn)行結(jié)果:

Android TextView實(shí)現(xiàn)圖文混合編排的方法

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI