Android TextView文字过多时通过滚动条显示多余内容并实时显示最下面那行
目前有两种实现方法
一.在TextView外面套一层ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="content">
<TextView
android:id="@+id/display_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Display time here"
android:textSize="20dp"
android:textColor="#ff0000"
android:background="#00ffaa"
/>
</ScrollView>
二、为TextView添加如下属性
android:scrollbars="vertical"
android:singleLine="false"
并添加如下代码:
mDisplayLog.setMovementMethod(ScrollingMovementMethod.getInstance());
采用如上的方法,TextView中的文字过对的时候就可以滚动查看了。
如果让TextView能够实时显示最后一行,不用手动去划,可以用如下代码实现:
int offset=mDisplayLog.getLineCount()*mDisplayLog.getLineHeight();
if (offset > mDisplayLog.getHeight()) {
mDisplayLog.scrollTo(0,offset - mDisplayLog.getHeight());
}
本文介绍两种在Android中使用TextView实现滚动显示长文本的方法。一种是在TextView外包裹ScrollView;另一种是直接为TextView设置垂直滚动条,并调用特定方法实现自动滚动到最新内容。
1684

被折叠的 条评论
为什么被折叠?



