Layoutopt工具是一款简单的命令行工具,它能够帮助你找到没必要要的控件嵌套以及缩减布局资源的其余方法,以便尽可能减小资源的使用。它让你能够了解哪些布局控件多是多余的或没必要要的。控件越少、布局层次越浅,性能就越好。java
运行layoutopt工具是至关简单的,只须要跟上一个布局文件或布局文件所在目录做为参数,须要注意的是,这里你必须包括布局文件或目录的完整路径,即便你当前就位于这个目录。咱们来看一个简单的例子:android
D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml
Layoutopt的输出结果只是建议,你能够有选择地在你的应用程序中采纳这些建议,下面来看几个使用layoutopt输出建议的例子。less
1. 无用的布局eclipse
在布局设计期间,咱们会频繁地移动各类组件,有些组件最终可能会再也不使用,如:工具
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content"></TextView> </LinearLayout> </LinearLayout>
工具将会很快告诉咱们LinearLayout内的LinearLayout是多余的:布局
11:17 This LinearLayout layout or its LinearLayout parent is useless
输出结果每一行最前面的两个数字表示建议的行号。性能
2.根能够替换测试
Layoutopt的输出有时是矛盾的,例如:优化
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content"></TextView> <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout> </FrameLayout>
这个布局将返回下面的输出:spa
5:22 The root-level <FrameLayout/> can be replaced with <merge/> 10:21 This LinearLayout layout or its FrameLayout parent is useless
第一行的建议虽然可行,但不是必需的,咱们但愿两个TextView垂直放置,所以LinearLayout应该保留,而第二行的建议则能够采纳,能够删除无用的FrameLayout。
有趣的是,这个工具不是全能的,例如,在上面的例子中,若是咱们给FrameLayout添加一个背景属性,而后再运行这个工具,第一个建议固然会消失,但第二个建议仍然会显示,工具知道咱们不能经过合并控制背景,但检查了LinearLayout后,它彷佛就忘了咱们还给FrameLayout添加了一个LinearLayout不能提供的属性。
3.太多的视图
每一个视图都会消耗内存,在一个布局中布置太多的视图,布局会占用过多的内存,假设一个布局包含超过80个视图,layoutopt可能会给出下面这样的建议:
-1:-1 This layout has too many views: 83 views, it should have <= 80! -1:-1 This layout has too many views: 82 views, it should have <= 80! -1:-1 This layout has too many views: 81 views, it should have <= 80!
上面给出的建议是视图数量不能超过80,固然最新的设备有可能可以支持这么多视图,但若是真的出现性能不佳的状况,最好采纳这个建议。
4.嵌套太多
布局不该该有太多的嵌套,layoutopt(和Android团队)建议布局保持在10级之内,即便是最大的平板电脑屏幕,布局也不该该超过10级,RelativeLayout多是一个解决办法,但它的用法更复杂,好在Eclipse中的Graphical Layout资源工具更新后,使得这一切变得更简单。
下面是布局嵌套太多时,layoutopt的输出内容:
-1:-1 This layout has too many nested layouts: 12 levels, it should have <= 10! 305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless 307:314 This LinearLayout layout or its FrameLayout parent is possibly useless 310:312 This LinearLayout layout or its LinearLayout parent is possibly useless
嵌套布局警告一般伴随有一些无用布局的警告,有助于找出哪些布局能够移除,避免屏幕布局所有从新设计。
小结
Layoutopt是一个快速易用的布局分析工具,找出低效和无用的布局,你要作的是判断是否采纳layoutopt给出的优化建议,虽然采纳建议做出修改不会当即大幅改善性能,但没有理由须要复杂的布局拖慢整个应用程序的速度,而且后期的维护难度也很大。简单布局不只简化了开发周期,还能够减小测试和维护工做量,所以,在应用程序开发期间,应尽早优化你的布局,不要等到最后用户反馈回来再作修改。