문제

how to get how many lines are displayed in visible part of TextView? I use text, which not fully placed in TextView on every screen resolution.

<?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);     
도움이 되었습니까?

해결책

android.text.Layout contains this information and more. Use textView.getLayout().getLineCount() to obtain line count.

Be wary that getLayout() might return null if called before the layout process finishes. Call getLayout() after onGlobalLayout() or onPreDraw() of ViewTreeObserver. E.g.

textView.getViewTreeObserver().addOnPreDrawListener(() -> {
    final int lineCount = textView.getLayout().getLineCount();
});

If you want only visible line count you should probably use the approach mentioned in the answer below:

Is there a way of retrieving a TextView's visible line count or range?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top