怎樣才能寫出優秀的Android App,是每一個程序員追求的目標。那么怎么才能寫出一個優秀的App呢?相信很多初學者也會有這種迷茫。一句話來回答這個問題:細節很重要。今天我們就從最基礎的XML布局來談談怎么提高Android性能問題吧!
也許你經常會遇到比較復雜的布局,這種情況下,最簡單的方法就是多層嵌套實現效果,但是最簡單的方法是否是最優的方法呢? 這里需要打一個大大的問號?????經驗告訴我們,往往簡單的方法,得到的結果不是最優解,那么我們通過一個例子來研究一下怎么去優化我們的XML布局吧,下面通過經典微信中的“發現”tab頁面中的布局來看看怎么實現。

上面這張圖片是微信界面截圖,看到這張效果圖的第一眼會讓開發者想到使用線性布局實現這種左邊圖片,右邊文字,一行白色背景效果很方便。那么我們就按照一般思路寫出如下布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_eeeeee" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/afe" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/fiends" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@android:color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/afg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/scan" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/color_e0e0e0"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/afh" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/shake" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@android:color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/afd" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/nearby" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/color_e0e0e0"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/afb" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/float_bottle" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@android:color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/agg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/shopping" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/color_e0e0e0"></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_bg_select" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ak6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="@string/games" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/ala" android:gravity="center" android:text="@string/weixin" android:textColor="@color/color_9e9e9e" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/al9" android:gravity="center" android:text="@string/countans" android:textColor="@color/color_9e9e9e" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/alc" android:gravity="center" android:text="@string/finds" android:textColor="@color/color_9e9e9e" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/ale" android:gravity="center" android:text="@string/me" android:textColor="@color/color_9e9e9e" /> </LinearLayout></LinearLayout>
以上布局的效果圖如下:

是不是差不多實現了微信一樣的效果?那么我們怎么來判斷以上布局是不是最優的呢?當然,我們是有工具來查看的。相信很多童鞋用過了,第一個就是 Hierarchy View,第二個就是 顯示GPU過度繪制。
Hierarchy View檢測布局嵌套層次
如果你是使用AS開發的話,你可以在 AS 工具欄中點擊 Tools 主站蜘蛛池模板: 深泽县| 陈巴尔虎旗| 平湖市| 瑞安市| 义乌市| 明溪县| 仁化县| 长垣县| 邵阳市| 灵台县| 耒阳市| 克东县| 沁阳市| 乐亭县| 平湖市| 长阳| 扶风县| 星座| 厦门市| 高青县| 襄城县| 岫岩| 宜丰县| 榆中县| 霞浦县| 仪征市| 两当县| 凤城市| 鹤壁市| 保山市| 宜春市| 获嘉县| 武夷山市| 绍兴县| 邯郸县| 牙克石市| 临夏县| 基隆市| 新巴尔虎左旗| 江都市| 杭州市|