无论是哪本讲解布局优化的参考书,它们都不得不提到Hierarchy Viewer。不过,通常情况下,Hierarchy( 英['haɪərɑːkɪ])Viewer无法在真机上进行使用,它只能在工厂的Demo机和模拟器上使用,即非加密过的设施。Google的大神——Romain Guy提供了一个开源项目View Server,通过这个程序可以让普通的手机也能使用Hierarchy Viewer,有兴趣的朋友可以前去理解一下,传送门:点击前往
下面在模拟器中使用这个工具,如图进入ADM:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.performanceoptimizationtest.HierarchyViewerTest"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hierarchyviewer"/> </LinearLayout> </LinearLayout></LinearLayout>
如此可以发现,只用三层LinearLayout嵌套,只装载了一个Button,很显然这些LinearLayout都是冗余的。下面利用工具进行分析:
通过Hierarchy Viewer工具,即可以很快地在视图树中找到冗余的布局,从而有目的地优化布局。同时,Hierarchy Viewer工具还可显示很多有用的信息,如下如: