现在的位置: 首页 > 综合 > 正文

Android 如何使用layoutopt检验布局文件是否合理

2013年07月07日 ⁄ 综合 ⁄ 共 1367字 ⁄ 字号 评论关闭

 Android 开发中,布局文件一般是不可缺少的,不良的布局文件【比如:不必要的嵌套,过多的视图,嵌套太深】,很容易引起系统开销过大,程序anp等等。怎么初步检测一个布局文件是否合理?除了总所周知的一些规则,我们在项目中践行之外,sdk 还包含了一个layoutopt命令行工具帮助我们分析布局文件的合理性。怎么使用呢?
在window平台下,可以通过layoutopt.bat快速地使用layoutopt分析功能。该bat文件位于android-sdk-windows\tools目录下。

假如该文件的目录是F:\Work\Tool\Android\android-sdk-windows\tools。

1、进入layoutopt.bat目录

1)在运行【win+r】中输入 cmd

2)进入tool目录


[plain]
C:\Documents and Settings\administrator>cd /d F:\Work\Tool\Android\android-sdk- 
windows\tools 
 
F:\Work\Tool\Android\android-sdk-windows\tools> 
2、检验布局文件E:\Code\git\Camera\res\layout\viewitem.xml

[plain]
F:\Work\Tool\Android\android-sdk-windows\tools>layoutopt.bat E:\Code\git\Camera\res\layout\viewitem.xml 
E:\Code\git\Camera\res\layout\viewitem.xml 

如果该布局文件合法将显示上面的结果
如果不合法将显示类似如下的:


[plain]
F:\Work\Tool\Android\android-sdk-windows\tools>layoutopt.bat E:\Code\git\Camera\res\layout\viewlist.xml 
E:\Code\git\Camera\res\layout\viewlist.xml 
        35:42 This tag and its children can be replaced by one <TextView/> and a 
 compound drawable 
        46:53 This tag and its children can be replaced by one <TextView/> and a 
 compound drawable 
        57:64 This tag and its children can be replaced by one <TextView/> and a 
 compound drawable 
F:\Work\Tool\Android\android-sdk-windows\tools> 

前面的数字是行数
3、传入布局文件所在的目录,批量检验布局文件


[plain]
F:\Work\Tool\Android\android-sdk-windows\tools>layoutopt.bat E:\Code\git\Camera\res\layout 

有了相关提示,我们可以 很方便地找出低效无用的布局,进行相应优化。当然,有的时候,提示不一定精准,需要我们适当地取舍。

【上篇】
【下篇】

抱歉!评论已关闭.