有一句古話:不論黑貓白貓,能抓到耗子就是好貓。這個也許在某些方面是有道理的,但對于我們追求精益求精的思想是背道而馳的,往往就是因為滿足于一個結(jié)果,而放棄探求更加優(yōu)化的處理方法。
當(dāng)關(guān)注應(yīng)用程序或者游戲所達到的結(jié)果時,往往非常容易忽視一些優(yōu)化的問題,例如內(nèi)存優(yōu)化,線程優(yōu)化,Media優(yōu)化和UI優(yōu)化等等。不同的模塊都存在更為巧妙的方式來對待一般性問題,所以每當(dāng)我們實現(xiàn)一個行為后,稍微多花一些時間來考慮目前所作的工作是否存在更為高效的解決辦法。 這一次我們對常用的UI Layout優(yōu)化說起,這個例子轉(zhuǎn)載于 android developing blog 在Android中最常用LinearLayout表示UI的框架,而且也是最直觀和方便的方法。例如創(chuàng)建一個UI用于展現(xiàn)Item的基本內(nèi)容。如圖所示:
直接可以通過LinearLayout快速的實現(xiàn)這個UI的排列:
View Code XML <LinearLayout xmlns: android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"
android:gravity="center_vertical" android:text="My Application" />
<TextView android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"
android:singleLine="true" android:ellipsize="marquee" android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
</LinearLayout> | 盡管可以通過Linearlayout實現(xiàn)我們所預(yù)想的結(jié)果,但是在這里存在一個優(yōu)化的問題,尤其是針對為大量Items。比較RelativeLayout和LinearLayout,在資源利用上前者會占用更少的資源而達到相同的效果,以下是用RelativeLayout實現(xiàn)的代碼:
View Code XML <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<TextView android:id="@+id/secondLine"
android:layout_width="fill_parent" android:layout_height="26dip"
android:layout_toRightOf="@id/icon" android:layout_alignParentBottom="true" android:layout_alignParentRight="true"
android:singleLine="true" android:ellipsize="marquee" android:text="Simple application that shows how to use RelativeLayout" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_above="@id/secondLine" android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical" android:text="My Application" />
</RelativeLayout> | 針對RelativeLayout有一點需要注意,因為它內(nèi)部是通過多個View之間的關(guān)系而確定的框架,那么當(dāng)其中某一個View因為某些需要調(diào)用GONE來完全隱藏掉后,會影響與其相關(guān)聯(lián)的Views。Android為我們提供了一個屬性 alignWithParentIfMissing 用于解決類似問題,當(dāng)某一個View無法找到與其相關(guān)聯(lián)的Views后將依據(jù)alignWithParentIfMissing的設(shè)定判斷是否與父級View對齊。 下邊是兩種不同layout在Hierarchy Viewer中的層級關(guān)系圖: |