Android TextView實(shí)現(xiàn)圖文混合編排的方法
本文實(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é)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中TextView和ImageView實(shí)現(xiàn)傾斜效果
- Android TextView實(shí)現(xiàn)帶鏈接文字事件監(jiān)聽的三種常用方式示例
- Android用戶輸入自動提示控件AutoCompleteTextView使用方法
- Android TextView對齊的兩種方法
- Android實(shí)現(xiàn)帶動畫效果的可點(diǎn)擊展開TextView
- Android TextView中部分文字高亮顯示
- Android自定義textview實(shí)現(xiàn)豎直滾動跑馬燈效果
- Android中TextView顯示圓圈背景或設(shè)置圓角的方法
- Android 自定義TextView去除paddingTop和paddingBottom
相關(guān)文章
Android使用ListView實(shí)現(xiàn)下拉刷新及上拉顯示更多的方法
這篇文章主要介紹了Android使用ListView實(shí)現(xiàn)下拉刷新及上拉顯示更多的方法,結(jié)合實(shí)例形式分析了ListView滾動刷新與加載的相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android中基于XMPP協(xié)議實(shí)現(xiàn)IM聊天程序與多人聊天室
這篇文章主要介紹了Android中基于XMPP協(xié)議實(shí)現(xiàn)IM聊天程序與多人聊天室的方法,XMPP基于XML數(shù)據(jù)格式傳輸,一般用于即時(shí)消息(IM)以及在線現(xiàn)場探測,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)3D層疊式卡片圖片展示
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D層疊式卡片圖片展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析(上)
這篇文章主要介紹了Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析,具有很好的參考價(jià)值,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤
這篇文章主要介紹了Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤,總結(jié)分析了Android開發(fā)中幫助文件、開發(fā)工具、社區(qū)等的重要性以及重要的開發(fā)原則,需要的朋友可以參考下2016-01-01
Android編程設(shè)計(jì)模式之Builder模式實(shí)例詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之Builder模式,結(jié)合實(shí)例形式詳細(xì)分析了Android設(shè)計(jì)模式之Builder模式概念、功能、使用場景、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
Android入門教程之RecyclerView的具體使用詳解
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動,也可以實(shí)現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2021-10-10
Android實(shí)現(xiàn)背景可滑動登錄界面 (不壓縮背景彈出鍵盤)
這篇文章主要介紹了Android實(shí)現(xiàn)背景可滑動登錄界面 (不壓縮背景彈出鍵盤),需要的朋友可以參考下2017-04-04
在Android中調(diào)用WebService實(shí)例
這篇文章主要介紹了在Android中調(diào)用WebService實(shí)例,有需要的朋友可以了解一下。2016-11-11

