好久没更新博客了,趁着清明来写点什么。

  今天来讲下如何使用android中提供的工具优化我们的布局。首先我们写一个最简单的框架布局。

  


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView 
android:layout_width="300dip" 
android:layout_height="300dip" 
android:background="#00008B"
android:layout_gravity="center"
/>
<TextView 
android:layout_width="250dip" 
android:layout_height="250dip" 
android:background="#0000CD"
android:layout_gravity="center"
/>
<TextView 
android:layout_width="200dip" 
android:layout_height="200dip" 
android:background="#0000FF"
android:layout_gravity="center"
/>
<TextView 
android:layout_width="150dip" 
android:layout_height="150dip" 
android:background="#00BFFF"
android:layout_gravity="center"
/>
<TextView 
android:layout_width="100dip" 
android:layout_height="100dip" 
android:background="#00CED1"
android:layout_gravity="center"
/>
</FrameLayout>

 非常简单的一个布局,实现一个层叠的效果,运行效果如下图:

下面我们就用android中提供的一个观察布局的工具,层级观察器,Hierarchy Viewer来观察我们的布局。Hierarchy Viewer工具是一个非常好的布局优化工具,同时,你也可以通过它学习他人的布局。应该说是一个非常实用的工具。

   Hierarchy Viewer在sdk的tools目录下,打开后最新界面如图所示:

  界面很简洁,列出了当前设备上的进程,在前台的进程加粗显示。上面有三个选项,分别是刷新进程列表,将层次结构载入到树形图,截取屏幕到一个拥有像素栅格的放大镜中。对应的在左下角可以进行三个视图的切换。在模拟器上打开写好的框架布局,在页面上选择,点击Load View,进入如图所示界面。

  左边的大图为应用布局的树形结构,上面写有控件名称和id等信息,下方的圆形表示这个节点的渲染速度,从左至右分别为测量大小,布局和绘制。绿色最快,红色最慢。右下角的数字为子节点在父节点中的索引,如果没有子节点则为0。点击可以查看对应控件预览图、该节点的子节点数(为6则有5个子节点)以及具体渲染时间。双击可以打开控件图。右侧是树形结构的预览、控件属性和应用界面的结构预览。点击相应的树形图中的控件可以在右侧看到他在布局中的位置和属性。工具栏有一系列的工具,保存为png或者psd,刷新等工具。其中有个load overlay选项可以加入新的图层。当你需要在你的布局中放上一个bitmap,你会用到它来帮你布局。点击左下角的第三个图标切换到像素视图,如下图所示。

  视图左侧为View和ViewGroup关系图,点击其中的View会在右边的图像中用红色线条为我们选中相应的View。最右侧为设备上的原图。中间为放大后带像素栅格的图像,可以在Zoom栏调整放大倍数。在这里能定位控件的坐标,颜色。观察布局就更加的方便了。

接下来再介绍下另一个布局优化工具-layoutopt。这是android为我们提供的布局分析工具。它能分析指定的布局,然后提出优化建议。  

  要运行它,打开命令行进入sdk的tools目录,输入layoutopt加上你的布局目录命令行。运行后如图所示,框出的部分即为该工具分析布局后提出的建议,这里为建议替换标签。

下面列出一些常会碰到的输出:

$ layoutopt samples/
samples/compound.xml
   7:23 The root-level <FrameLayout/> can be replaced with <merge/>
   11:21 This LinearLayout layout or its FrameLayout parent is useless
samples/simple.xml 提示未使用到该布局
   7:7 The root-level <FrameLayout/> can be replaced with <merge/>
samples/too_deep.xml
   -1:-1 This layout has too many nested layouts: 13 levels, it should have <= 10!
   20:81 This LinearLayout layout or its LinearLayout parent is useless
   24:79 This LinearLayout layout or its LinearLayout parent is useless
   28:77 This LinearLayout layout or its LinearLayout parent is useless
   32:75 This LinearLayout layout or its LinearLayout parent is useless
   36:73 This LinearLayout layout or its LinearLayout parent is useless
   40:71 This LinearLayout layout or its LinearLayout parent is useless
   44:69 This LinearLayout layout or its LinearLayout parent is useless
   48:67 This LinearLayout layout or its LinearLayout parent is useless
   52:65 This LinearLayout layout or its LinearLayout parent is useless
   56:63 This LinearLayout layout or its LinearLayout parent is useless
samples/too_many.xml
   7:413 The root-level <FrameLayout/> can be replaced with <merge/>
   -1:-1 This layout has too many views: 81 views, it should have <= 80!
samples/useless.xml 提示该布局中有太多的控件
   7:19 The root-level <FrameLayout/> can be replaced with <merge/>
   11:17 This LinearLayout layout or its FrameLayout parent is useless

通过这个工具,能很好的优化我们的UI设计,布局方法。好了,今天就写到这里了。希望对大家有帮助,有问题可以留言交流~这里说下有的网站转载了我的文章,我很开心。但是都不留个出处,那就不太好了。所以欢迎转载,但是请保留出处。http://www.cnblogs.com/noTice520/

利用Hierarchy Viewer优化布局的更多相关文章

  1. 看了一本书,说可以利用Hierarchy Viewer优化布局

    看了一本书,说可以利用Hierarchy Viewer优化布局,今以志之. 参考:http://www.cnblogs.com/Rocky_/archive/2011/11/04/2236243.ht ...

  2. Android 卡顿优化 3 布局优化 工具 Hierarchy Viewer

    欲善其事, 先利其器. 分析布局, 就不得不用到Hierarchy Viewer了. 本文工具使用皆以GithubApp的详情界面RepoDetailActivity为例说明. 为了不影响阅读体验, ...

  3. Android 性能优化(4)Optimizing Layout Hierarchies:用Hierarchy Viewer和Layoutopt优化布局

    Optimizing Layout Hierarchies This lesson teaches you to Inspect Your Layout Revise Your Layout Use ...

  4. Android优化——UI检视利器:Hierarchy Viewer

    在Android的SDK工具包中,有很多十分有用的工具,可以帮助程序员开发和测试Android应用程序,大大提高其工作效率.其中的一款叫 Hierachy Viewer的可视化调试工具,可以很方便地在 ...

  5. Android 性能优化(22)*性能工具之「Hierarchy Viewer」 Hierarchy Viewer Walkthrough

    Hierarchy Viewer Walkthrough 1.In this document Prerequisites Setting the ANDROID_HVPROTO variable W ...

  6. Android 性能优化(2)性能工具之「Hierarchy Viewer 」Optimizing Your UI:分析哪个view有性能问题,查看屏幕上某像素点的坐标,颜色等

    Optimizing Your UI In this document Using Hierarchy Viewer Running Hierarchy Viewer and choosing a w ...

  7. Android中View绘制优化之一---- 优化布局层次

    本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 前言,竟然是翻译,当然得弄的有板有眼. 照着大作家格式来咯 , - - . 译序 最近一直在做锁屏界面,之前也 ...

  8. Hierarchy Viewer

    http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下,名为h ...

  9. 【转】【Android工具】被忽略的UI检视利器:Hierarchy Viewer

    原文:http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下, ...

随机推荐

  1. word异常关闭,找到丢失的word

    Word模板路径位置 XP系统用户默认模板路径一般在 C:\Documents and Settings\Administrator\Application Data\Microsoft\Templa ...

  2. 3.1 Data Member的绑定

       文章开始提出了一段示例代码,并讨论了返回哪个x的问题.然后 a)   给出了我们普遍认为正确的回答,并肯定了这个想法: b)   提醒大家,这在以前的编译器实现中,并非正确. 在早期的实现中,首 ...

  3. Python in minute

    Python 性能优化相关专题:    https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/   Python wikipedi ...

  4. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  5. 学习笔记——中介者模式Mediator

    中介者模式,A与B之间存在调用对方操作的行为,通过中介者模式,可以去除双方的强引用.如图,A可以通过代理Mediator的方法来调用B的方法,而不需要保存B的引用.

  6. django搭建Bootstrap常用问题解决方法

    1.进入页面,提示Creating a ModelForm without either the 'fields' attribute or the 'exclude'时 解决方法:打开forms.p ...

  7. 读入一行字符给string类型

    小技巧 string ss: getline(cin,ss):

  8. 关于Adobe CC 系列软件反复弹出 Adobe 软件许可问题

    最近装了falsh cc2014 版本,并结合网络提供资源破解(可参考:http://www.cnblogs.com/-yan/p/4450196.html) 但是,使用超过二十四小时后(时间为估计值 ...

  9. pureMVC java版搭建流程

    转自:http://blog.csdn.net/sutaizi/article/details/6588004 pureMVC 是一个轻量级的框架 它在 flex中非常流行(和cairngorm差不多 ...

  10. Ubuntu 下安装 nfs

    1先安装sudo apt-get install nfs-kernel-server(安装nfs-kernel-server时,apt会自动安装nfs-common和portmap) 分开看就是1.服 ...