android图文混合布局,Android TextView图文混合编排详解

Android TextView图文混合编排

实现技术细节不难,两个要点:

1、html代码的混合编写。

2,重写ImageGetter。

例如:

布局:<?xml  version="1.0" encoding="utf-8"?>

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">

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/text3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ellipsize="end"

android:maxLines="1" />

android:id="@+id/text4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ellipsize="end"

android:maxLines="1" />

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 " + "" + " " + "" + " " + "" + " "; 

}

private String stringMixWithImage2(String string) {

        return "2 " + "" + " " + "" + " " + "" + " " + string; 

}

private String stringMixWithImage3(String string) {

        return string + "3 " + "" + " " + "" + " " + "" + " "; 

}

private String stringMixWithImage4(String string) {

        return "4 " + "" + " " + "" + " " + "" + " " + 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;

}

};

}

代码运行结果:

fff6c98081bf8904098fb5e1bd59ff4b.gif

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值