海南网站建设平台,dedecms做企业网站,wordpress添加多个菜单,宁波建站方案使用TextView的getLineCount方法#xff0c;它可以返回TextView当前显示的行数。但是#xff0c;这个方法只有在TextView绘制完成后才能返回正确的值#xff0c;否则可能返回0。因此#xff0c;需要在TextView的post方法中调用#xff0c;或者在onWindowFocusChanged方法中…使用TextView的getLineCount方法它可以返回TextView当前显示的行数。但是这个方法只有在TextView绘制完成后才能返回正确的值否则可能返回0。因此需要在TextView的post方法中调用或者在onWindowFocusChanged方法中调用。
// 在TextView的post方法中调用
textView.post(new Runnable() {Overridepublic void run() {int lineCount textView.getLineCount(); // 获取行数Log.d(TAG, lineCount lineCount);}
});// 在onWindowFocusChanged方法中调用
Override
public void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);if (hasFocus) {int lineCount textView.getLineCount(); // 获取行数Log.d(TAG, lineCount lineCount);}
}
这种方法不需要实例化TextView也不需要等待TextView绘制完成只需要提供与TextView相同的参数即可。
// 使用StaticLayout类
String text textView.getText().toString(); // 获取文本内容
TextPaint textPaint textView.getPaint(); // 获取文本画笔
int width textView.getWidth(); // 获取文本宽度
StaticLayout staticLayout new StaticLayout(text, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); // 创建StaticLayout对象
int lineCount staticLayout.getLineCount(); // 获取行数
Log.d(TAG, lineCount lineCount);// 使用DynamicLayout类
String text textView.getText().toString(); // 获取文本内容
TextPaint textPaint textView.getPaint(); // 获取文本画笔
int width textView.getWidth(); // 获取文本宽度
DynamicLayout dynamicLayout new DynamicLayout(text, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); // 创建DynamicLayout对象
int lineCount dynamicLayout.getLineCount(); // 获取行数
Log.d(TAG, lineCount lineCount);