我想知道 textView 可以显示的数轴。
我可以在一行中获取数字字符
int maxVisibleChars = textView.getPaint().breakText(text,
true, textView.getMeasuredWidth(), null);
但我不知道我的textView可以正确显示多少行
因为我的 textView 调整大小取决于屏幕分辨率。
我需要知道这一点。谢谢
我使用此代码来检查我的文本视图中有多少行,
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
int totalLines = textView.getLineCount();
ViewTreeObserver vto = this.textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
ViewTreeObserver obs = textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
System.out.println("Line Count is : " + textView.getLineCount());
}
});
}
}
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="500dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="I have a full-screen TextView holding a long Spanned that requires scrolling. The TextView's getLineCount() gives me the total number of lines used for the entire block of text but I'd like to know how many lines of text are currently visible on the screen. "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
如果您使用带滚动的 Textview 并且您需要知道屏幕上显示的行数(而不是总行数),
int height = myTextView.getHeight();
int scrollY = myTextView.getScrollY();
Layout layout = myTextView.getLayout();
int firstVisibleLineNumber = layout.getLineForVertical(scrollY);
int lastVisibleLineNumber = layout.getLineForVertical(scrollY+height);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)