原文連結:https://blog.csdn.net/u014341735/article/details/78425437
目前,項目需求中有提到這個,網上找了一些,效果都不理想,不能很好的實作滾動,是以在此将自己實作的方式貼出來,以後或許還會用到!
- 1
實作的兩種方式
1.使用TextView實作
通過在XML和代碼設定
XML代碼如下:
<TextView
android:id="@+id/txt_showinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="insideOverlay"
android:fadeScrollbars="false"
android:scrollbarFadeDuration="2000"/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
代碼裡進行如下設定:
txt_mmm = (TextView) findViewById(R.id.txt_showinfo);
txt_mmm.setMovementMethod(ScrollingMovementMethod.getInstance());
txt_mmm.post(new Runnable() {
@Override
public void run() {
txt_mmm.append(showString);
int scrollAmount = txt_mmm.getLayout().getLineTop(txt_mmm.getLineCount())
- txt_mmm.getHeight();
if (scrollAmount > )
txt_mmm.scrollTo(, scrollAmount);
else
txt_mmm.scrollTo(, );
}
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2.使用ScrollView和TextView共同實作
通過在XML和代碼設定
XML代碼如下:
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/txt_info"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"
android:fadeScrollbars="false"
android:scrollbarFadeDuration="2000"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</ScrollView>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
代碼裡進行如下設定:
scrollView = (ScrollView) findViewById(R.id.scrollview);
txt_info = (TextView) findViewById(R.id.txt_info);
txt_info.setMovementMethod(ScrollingMovementMethod.getInstance());
txt_info.append(showString);
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(, txt_info.getBottom());
}
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
注意:兩種方式雖能實作這個效果,但具體還是有些許差異,待後續測試完畢再給出結論。
參考位址:http://ask.csdn.net/questions/107/