A few weeks ago I discovered the Spans on Android,after reading the wonderful post by Flavien Laurent.

In this post I will describe how to realize a particular layout not very common on Android: a text around an image.

This layout is not an Android Pattern, but it can be useful in same cases.
As always it is just an example, and you should improve some points in your real project.

Use a simple layout:

    <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView> <ImageView
android:id="@+id/icon"
android:src="@drawable/rectangle"
android:layout_width="150dp"
android:layout_height="150dp"></ImageView> </RelativeLayout> </ScrollView>

To achieve our scope, we can use a LeadingMarginSpan.LeadingMarginSpan2.
This span allows the implementor to specify the number of lines of text to which this object is attached that the "first line of paragraph" margin width will be applied to.

    /**
*
*/
class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 { private int margin;
private int lines; MyLeadingMarginSpan2(int lines, int margin) {
this.margin = margin;
this.lines = lines;
} /**
* Apply the margin
*
* @param first
* @return
*/
@Override
public int getLeadingMargin(boolean first) {
if (first) {
return margin;
} else {
return 0;
}
} @Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
int top, int baseline, int bottom, CharSequence text,
int start, int end, boolean first, Layout layout) {} @Override
public int getLeadingMarginLineCount() {
return lines;
}
};

We only need to calculate the number of lines where we would like applying a margin and the right margin.
In this case we will get number of lines = height of image and margin = width of image + little extra margin.

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.text);
mImageView = (ImageView) findViewById(R.id.icon); final ViewTreeObserver vto = mImageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
finalHeight = mImageView.getMeasuredHeight();
finalWidth = mImageView.getMeasuredWidth();
makeSpan();
}
});
}
}

This code can be improved.
I am using a very simple (and raw) float textLineHeight = mTextView.getPaint().getTextSize(); to calculate the number of lines.
You can add paddings, margins or you can use a Rect to calculate the text bounds.

    /**
* This method builds the text layout
*/
private void makeSpan() { /**
* Get the text
*/
String plainText=getResources().getString(R.string.text_sample); int allTextStart = 0;
int allTextEnd = htmlText.length() - 1; /**
* Calculate the lines number = image height.
* You can improve it... it is just an example
*/
int lines;
Rect bounds = new Rect();
mTextView.getPaint().getTextBounds(plainText.substring(0,10), 0, 1, bounds); //float textLineHeight = mTextView.getPaint().getTextSize();
float fontSpacing=mTextView.getPaint().getFontSpacing();
lines = (int) (finalHeight/fontSpacing); /**
* Build the layout with LeadingMarginSpan2
*/
MyLeadingMarginSpan2 span = new MyLeadingMarginSpan2(lines, finalWidth +10 );
mSpannableString.setSpan(span, allTextStart, allTextEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mTextView.setText(mSpannableString);
}

You can get code from GitHub:

Snippet: align a TextView around an image的更多相关文章

  1. textview 显示html方法解析

    现在网络的繁盛时代,光文字是不能满足人们的胃口的,图片,flash,音频,视频就成为浏览网页的主流显示,在手机上也一样.在手机上显示从网络端获取的数据显示,大家很自然的想起两种方式,一种就是webvi ...

  2. Android之TextView文字绘制流程

    一:TextView的onDraw()方法: 1.第一句restartMarqueeIfNeeded()绘制字幕滚动. protected void onDraw(Canvas canvas) { r ...

  3. TextView里的文 html

    一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...

  4. 自定义控件 TextView 歌词 Lrc

    演示 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < ...

  5. Android的TextView使用Html来处理图片显示、字体样式、超链接等

    一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...

  6. Android 获取View的高度或TextView的行数, 实现自适应的textview

    大家都遇到过项目中需要获控件的的高度或者列如文章开头说TextView的行数 但是很多人在实际操作中getLineCount()获取到值是零,其实只是我们没在正确的位置获取. 这是因为activtiy ...

  7. 【转载】TextView源码解析

    原文地址:https://github.com/7heaven/AndroidSdkSourceAnalysis/blob/master/article/textview%E6%BA%90%E7%A2 ...

  8. 关于textview显示特殊符号居中的问题

    话说这是2017年的第一篇博客,也是一篇技术博客.先从简单的一篇解决问题开始吧,千里之行,始于足下! ------------------------------------------------- ...

  9. 奇葩问题-TextView无法获取值

    问题场景 前几天写一个界面的时候,遇到一个非常奇葩的问题.app第一次安装的时候,这里针对用户第一次安装的时候,后来是不会出现这个问题了.我明明是对某个界面的一个textview赋值了,而且服务端也返 ...

随机推荐

  1. [转]oracle 10g数据泵之impdp-同时导入多个文件

    要了解impdp,请先了解导出,我之前作过导出的笔记:oracle 10g数据泵之expdp.这两个笔记也许只对程序员有用,通常用于把正式区的数据导入到测试区,对数据库管理员也许帮助不大,他们使用这些 ...

  2. Log4j 配置数据库连接池(将日志信息保存到数据库)

    org.apache.log4j.jdbc.JDBCAppender 是利用传统的 JDBC 连接方法,这种方式连接数据库效率低下,为了解决这个问题,现在自定义一个 Log4j 的 Appender, ...

  3. C++ 添加库

    三步走:(第三步总忘) 1.项目属性页的C/C++->常规->附加包含目录指向 2.链接器(Linker)->常规(general)->附加库目录指向 3.链接器->输入 ...

  4. Spring MVC静态资源处理(在applicationContex.xml文件中进行配置)

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  5. Address already in use: bind

    Eclipse中报了这个错误,下拉小窗口,可以看到正在运行的项目,选中项目,都关闭就Ok了 还有一种方法就是关闭javaw.exe进程

  6. GNU libc (Glibc) 2.18 发布

    标准C库Glibc发布2.18正式版.2013-08-12 上一个版本是2012-12-25的2.17. 详细改进:Version 2.18 * The following bugs are reso ...

  7. 盘点国内网站常用的一些 CDN 公共库加速服务

    CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将JS库存放在服务器单机上相比,CDN公共库更加稳定.高速.一 般的CDN公共库都会包含全球所有最流行的开源JavaScri ...

  8. [ACM_模拟] The Willy Memorial Program (poj 1073 ,联通水管注水模拟)

    Description Willy the spider used to live in the chemistry laboratory of Dr. Petro. He used to wande ...

  9. Windows下使用Redis(一)安装使用

    一.Redis 是什么 Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store).它通常被称为数据结构服务器,因为值(value)可以是 字符串(S ...

  10. linxu ffmpeg 编译安装

    1.下载ffmpeg. 下载网址:http://www.ffmpeg.org/download.html 2.解压缩 tar -zxvf ffmpeg-2.0.1.tar.gz 3.配置,生成Make ...