【发布时间】:2011-11-18 18:19:10
【问题描述】:
我在使用包含 153 个 TextView 的 RelativeLayout(包装在 ScrollView 中)时遇到性能问题。每个 TextView 都有 9 个补丁背景。
主要布局:
<ScrollView
android:id="@+id/forum_accueil_ScrollView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@color/white">
<RelativeLayout android:id="@+id/forum_accueil_RelativeLayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@color/white">
</RelativeLayout>
</ScrollView>
RelativeLayout 以编程方式填充了 TextView(项目计数和描述来自 WS)。每个 TextView 都有一个 9patch 背景选择器。 TextView 说明如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="35dip"
android:textColor="@color/myblue"
android:textStyle="bold"
android:gravity="center_vertical" android:layout_gravity="center_vertical"
android:background="@drawable/selector_forum_accueil_item">
</TextView>
最后,我有一个包含 153 个 TextView 的 RelativeLayout。显示和滚动真的很慢。即使重新开始这项活动也需要一些时间。
我打开了层次结构查看器,滚动视图需要以下时间才能显示:
测量:28 毫秒 布局:1.2ms 绘制:18ms
有没有办法加快显示和响应速度?包含尽可能多的 View 的单个 RelativeLayout 是否是最好的方法(我试图用这种技术尽可能地扁平化层次结构)? 我尝试删除 9 补丁背景进行测试,它似乎响应更快。为什么?
【问题讨论】:
-
速度很慢,因为您绘制了 153 次视图!适配器的优点在于它们只绘制当时需要的东西。您正在绘制您的所有视图,此时绝大多数都没有被查看。