如何确定在 Android 中的 TextView 中可以容纳多少文本? [英] How do I determine how much text will fit in a TextView in Android?

查看:25
本文介绍了如何确定在 Android 中的 TextView 中可以容纳多少文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局看起来像这样:

I have a layout that looks something like this:

[TextView 1] [TextView 2]
[ TextView 2 spill-over ]

本质上,我需要将 TextView 2 的内容换行到下一行,但从 TextView 1 开始的地方开始.我在想,如果我知道 TextView 2 在第一行空间不足之前可以容纳多少文本,我可以将其余文本放入前两个下方的另一个 TextView 中.所以我需要测量有多少文本可以放入 TextView (这可能很棘手,因为据我所知,Android 会尝试在 TextView 中的一个好位置打破文本,这样它就不会在中间如果可以避免)或者我需要一个关于如何布局的新想法.

Essentially, I need the contents of TextView 2 to wrap to the next line, but start where TextView 1 starts. I was thinking that if I knew how much text would fit into TextView 2 before it runs out of space on line one, I could take the rest of the text and put it in another TextView below the first two. So I need to measure how much text will fit into a TextView (which can be tricky because as far as I can tell, Android will try to break the text in a TextView at a good location so that it won't break a word in the middle if it can be avoided) or I need a new idea on how to lay this out.

任何帮助将不胜感激.

提前致谢,

帅气

推荐答案

不幸的是,Paint.breakText 没有返回与在我的两行 TextView 中看到的完全相同的结果.

Unfortunately, Paint.breakText didn't return the exact same result as in was seen in my two-line TextView.

但是,这行得通

int numChars = textView.getLayout().getLineEnd(1);

(使用 numberOfLines - 1 作为参数)

(use numberOfLines - 1 as the parameter to it)

参考http://developer.android.com/reference/android/text/Layout.html#getLineEnd(int)将TextView的单行设置为不同的宽度

这篇关于如何确定在 Android 中的 TextView 中可以容纳多少文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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