如何确定TextView中可见的行数?

umn*_*4ek 18 android lines textview

如何在可见部分显示多少行TextView?我使用的文字并没有完全放在TextView每个屏幕分辨率上.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/logs_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

String s = "very big text"
TextView logText = (TextView) view.findViewById(R.id.logs_text);
logText.setText(s);     
Run Code Online (Sandbox Code Playgroud)

Yar*_*lyk 20

android.text.Layout包含此信息等.使用textView.getLayout().getLineCount()以获得线路数.

提防getLayout()可能返回null如果调用布局过程完成之前.通话getLayout()onGlobalLayout()onPreDraw()ViewTreeObserver.例如

textView.getViewTreeObserver().addOnPreDrawListener(() -> {
    final int lineCount = textView.getLayout().getLineCount();
});
Run Code Online (Sandbox Code Playgroud)

如果您只想要可见的行数,您应该使用下面答案中提到的方法:

有没有办法检索TextView的可见行数或范围?