Android TextView 和获取文本行 [英] Android TextView and getting line of text

查看:19
本文介绍了Android TextView 和获取文本行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextView,它的文本可以运行到多行.一旦它被动态创建和设置,我想

I have a TextView whose text can run to many lines. Once it has been created and set dynamically, I'd like to

  1. 获取给定行上的文本,然后
  2. 知道这条线的宽度和高度.

我该怎么做?

推荐答案

如果我理解正确,问题2的答案是:

If I understand correctly the answer to question 2 is:

textView.getLineBounds (int line, Rect bounds)

像素宽度应该是abs(bounds.right - bounds.left);高度为 abs(bounds.bottom - bounds.top)

The width in pixels should be abs(bounds.right - bounds.left); and the height is abs(bounds.bottom - bounds.top)

你的第一个问题有点棘手,但这样的事情应该可以发挥所需的魔力:

Your first question is a bit more tricky, but something like this should do the required magic:

Layout layout = textView.getLayout();
String text = textView.getText().toString();
int start=0;
int end;
for (int i=0; i<textView.getLineCount(); i++) {
    end = layout.getLineEnd(i);
    line[i] = text.substring(start,end);
    start = end;
}

这篇关于Android TextView 和获取文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆