加速复杂的 Android View Inflation
我有一个相对较大的 XML 布局(38 kB,600 行),其层次结构如下:
<ScrollView>
<LinearLayout>
<TabHost>
<LinearLayout>
<FrameLayout> //tab widget
<LinearLayout> //tab content
<LinearLayout> //section
<TextView> //section name
<LinearLayout orientation="horizontal"> //item 1 box
<TextView> //item 1 title
<Spinner> //item 1 picker
</LinearLayout>
<LinearLayout> //item 2 box
<TextView> //item 2 title
<Spinner> //item 2 picker
</LinearLayout>
... //18 other items
</LinearLayout>
... //4 other sections with 15 items each
</>
它是一个数据输入表单,必须有那么多项目,我现在能做的最好的就是包装 setContentView
并通过“正在加载...”对话框将数据加载到 AsyncTask 中的微调器。
主题的广泛使用是否也会减缓观看次数膨胀?视图充气器不需要查看加载的 theme.xml,但如果我将主题内联到布局 xml 中,它也会显着增加 XML 的大小,从而减慢解析器的速度。
我可以做些什么来简化布局,使其加载速度至少提高两倍吗? 我想我可能会尝试摆脱水平 LinearLayouts,并使用 TableLayout 构建“部分”。
I have a relatively big XML layout (38 kB, 600 lines) with hierarchy like:
<ScrollView>
<LinearLayout>
<TabHost>
<LinearLayout>
<FrameLayout> //tab widget
<LinearLayout> //tab content
<LinearLayout> //section
<TextView> //section name
<LinearLayout orientation="horizontal"> //item 1 box
<TextView> //item 1 title
<Spinner> //item 1 picker
</LinearLayout>
<LinearLayout> //item 2 box
<TextView> //item 2 title
<Spinner> //item 2 picker
</LinearLayout>
... //18 other items
</LinearLayout>
... //4 other sections with 15 items each
</>
It is a data entry form that has to have that many items and the best I can do now is to do to wrap setContentView
and loading of data to the spinners in an AsyncTask with a "Loading..." dialog.
Does extensive using of themes also slow down the view inflation? The view inflater wouldn't need to look in loaded theme.xml, but if I were to inline the theme into the layout xml, it would also increase the size of the XML considerably, thus slow down the parser.
Is there something I could do to simplify the layout that would make it load at least twice as fast?
I'm thinking that I might try to get rid of the horizontal LinearLayouts, and build the "section" with TableLayout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,三个一般性评论:
RelativeLayout
来替换嵌套的LinearLayouts
。layout_opt
工具获取一些有关布局优化的建议。现在,更多关于#1 的内容……您是否考虑过编写自己的布局?乍一听起来很复杂,但有时可以极大地提高布局性能。例如,如果
RelativeLayout
不能达到目的,您可以将 Linear-Frame-Linear-Linear 链组合到单个自定义布局中。最后,您的选项卡指示器可滚动是否有原因?这似乎是一个导航/用户体验问题。
First, three general comments:
RelativeLayout
to replace nestedLinearLayouts
.layout_opt
tool to get some suggestions on layout optimizations.Now, more on #1 … have you considered writing your own layout? It sounds complex at first, but it can sometimes drastically improve layout performance. For example, if
RelativeLayout
doesn't serve the purpose, you may be able to combine your Linear-Frame-Linear-Linear chain into a single custom layout.Lastly, is there a reason your tab indicators are scrollable? That seems like a navigation/UX problem.
我唯一能想到的就是将您的视图分成几个不同的部分,并在用户到达该部分时膨胀每个视图。不确定这是否有帮助,但祝你好运。
也可能有助于查看traceview中的那部分代码
The only thing I can think to do would be to break your views into a couple different parts and inflate each view whenever the user gets to that part. Not sure if that helps at all but good luck.
Also might help to look at that portion of the code in traceview