Android TextView图文混合编排

简介: Android TextView图文混合编排实现技术细节不难,两个要点:1、html代码的混合编写。2,重写ImageGetter。
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), 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;
        }
    };
}



代码运行结果:


相关文章
|
16天前
|
Android开发
Android经典实战之Textview文字设置不同颜色、下划线、加粗、超链接等效果
本文介绍了 `SpannableString` 在 Android 开发中的强大功能,包括如何在单个字符串中应用多种样式,如颜色、字体大小、风格等,并提供了详细代码示例,展示如何设置文本颜色、添加点击事件等,助你实现丰富文本效果。
55 3
|
3月前
|
XML IDE 开发工具
13. 【Android教程】文本框 TextView
13. 【Android教程】文本框 TextView
68 2
|
3月前
|
编解码 Android开发
Android 解决TextView多行滑动与NestedScrollView嵌套滑动冲突的问题
Android 解决TextView多行滑动与NestedScrollView嵌套滑动冲突的问题
45 0
|
4月前
|
Android开发
android TextView HTML 的效果
android TextView HTML 的效果
33 2
|
4月前
|
XML 搜索推荐 Java
Android TextView的字体设置
【5月更文挑战第13天】
156 0
|
4月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
436 1
|
4月前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
85 0
|
Java Android开发 iOS开发
Android TextView 与 EditText 的区别与使用
Android TextView 与 EditText 的区别与使用
137 0
|
XML Android开发 数据格式
Android 中使用SpannableString实现TextView文本超链接跳转功能
Android 中使用SpannableString实现TextView文本超链接跳转功能
202 0
|
Android开发
Android 实现视图文本TextView的展开与收缩功能
Android 实现视图文本TextView的展开与收缩功能
269 0