Android工具layoutopt

Layoutopt工具是一款简单的命令行工具,它可以帮助你找到不必要的控件嵌套以及缩减布局资源的其他方法,以便尽量减少资源的使用。它让你可以了解哪些布局控件可能是多余的或不必要的。控件越少、布局层次越浅,性能就越好。

运行layoutopt工具是相当简单的,只需要跟上一个布局文件或布局文件所在目录作为参数,需要注意的是,这里你必须包括布局文件或目录的完整路径,即使你当前就位于这个目录。我们来看一个简单的例子:

D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml

Layoutopt的输出结果只是建议,你可以有选择地在你的应用程序中采纳这些建议,下面来看几个使用layoutopt输出建议的例子。

1. 无用的布局

在布局设计期间,我们会频繁地移动各种组件,有些组件最终可能会不再使用,如:

<?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>

这个布局将返回下面的输出:

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给出的优化建议,虽然采纳建议作出修改不会立即大幅改善性能,但没有理由需要复杂的布局拖慢整个应用程序的速度,并且后期的维护难度也很大。简单布局不仅简化了开发周期,还可以减少测试和维护工作量,因此,在应用程序开发期间,应尽早优化你的布局,不要等到最后用户反馈回来再做修改。

转载于:https://my.oschina.net/u/1175007/blog/486627

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值