不好的方法
安卓的TextView,一页放不下,就需要用到滚动功能,代码如下,加入ScrollView即可。
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textview_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:text="hello_first_fragment" />
</ScrollView>
参考: Android设置TextView可滚动_互联网小熊猫的博客-优快云博客_android textview 滚动
总结:这个方法虽然可以使用,但是滚动的效果很不好,PASS.
使用ScrollView和TextView共同实现
参考:Android TextView更新内容后自动滑行到最后一行_我又来瞟代码了的博客-优快云博客
这个方法很好。能完全滚到最后。
XML代码:其实就是加一个ID即可。
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:scrollbars="vertical"
android:fadeScrollbars="false"
>
初始化代码:
static public TextView m_TextView_Display = null;
static public ScrollView m_ScrollView = null;
m_TextView_Display = findViewById(R.id.textview_first);
m_ScrollView = (ScrollView) findViewById(R.id.scrollview);
调用代码,在显示完后,顺便滚动一下:
m_TextView_Display.append(str);
m_ScrollView.smoothScrollTo(0, m_TextView_Display.getBottom()+100);