Android的TextView的和文字让行 [英] Android TextView and getting line of text

查看:122
本文介绍了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)

您的第一个问题是比较麻烦一些,但这样的事情应该做必要的魔力:

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();
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天全站免登陆