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());
}