我的应用程序使用 StringBuilder 组合文本段落,然后显示在 ScrollView 内的 TextView 中。
displaytext.xml 布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/display_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
>
</TextView>
</ScrollView>
</LinearLayout>
显示StringBuilder对象sbText的代码是
setContentView(R.layout.displaytext);
TextView tv = (TextView)findViewById(R.id.display_text);
tv.setText(Html.fromHtml(sbText.toString()));
这工作正常,除了随着文本量的增加它变得非常慢。例如,要显示 50 个段落,总共约 50KB 的文本,仅执行这三行代码就需要 5 秒以上。
有人可以建议我如何加快速度吗?
最佳答案
Can anyone suggest how I can speed this up, please?
使用 WebView
而不是 ScrollView
和 TextView
。
此外,正如 Nick 所指出的,通过使用 Traceview 或其他工具,确认您所花的时间确实花在了您认为的地方。
关于Android:加快(html 格式)文本的显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4618605/