首页 > 解决方案 > 安卓。文本视图。文本的最后一个可见字符

问题描述

我有一个针对整个屏幕的 TextView 匹配,并在其上附加了一个文本。例如,乌鸦诗:

Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"'Tis some visiter," I muttered, "tapping at my chamber door—
Only this, and nothing more." etc etc etc...

我无法在手机屏幕上看到所有内容。所以问题是:如何获得文本最后一个可见字符的索引? 我怎么能看到它

标签: androidstringkotlintextviewmobile-development

解决方案


非常感谢Mike M这里的答案:

fun TextView.getLastVisibleCharacter(): Int {
        val layoutBottom = height + scrollY - totalPaddingTop - totalPaddingBottom
        val bottomLine = layout.getLineForVertical(layoutBottom)
        val lastWhollyVisibleLineNumber =
            if (layout.getLineBottom(bottomLine) > layoutBottom) bottomLine - 1
            else bottomLine
        return layout.getLineVisibleEnd(lastWhollyVisibleLineNumber) - 1
    }

推荐阅读