Android布局技巧之創(chuàng)建高效布局
Android UI工具包提供了一些布局管理器,它們使用起來相當(dāng)容易,而且,大多數(shù)的時(shí)候,你只需要使用它們最基本的特征來實(shí)現(xiàn)UI。
執(zhí)著于基本特征的使用對于創(chuàng)建UI來說,往往不是最高效的。一個常見的例子就是濫用LinearLayout,它將會導(dǎo)致View樹中的View數(shù)量激增。View——更糟的是,布局管理器——添加到應(yīng)用程序里都會帶來一定的消耗:初始化,布局和繪制變得更加緩慢。嵌套布局的花銷尤其“昂貴”,例如,如果你嵌套了一些LinearLayout,并使用了weight參數(shù),這會導(dǎo)致子元素要計(jì)算兩次。
讓我們看一個非常簡單且常見的布局例子:一個列表項(xiàng),左邊是一個圖標(biāo),右邊是標(biāo)題和描述,上方是標(biāo)題,下方是可選的描述。列表項(xiàng)可能看起來如下圖:
為了清楚地認(rèn)識View之間(一個ImageView和兩個TextView)的相對位置,下圖是使用HierarchyViewer抓獲的布局剪影:
實(shí)現(xiàn)這個布局,直接使用LinearLayout就可以了。列表項(xiàng)本身是一個水平的LinearLayout,里面有一個ImageView和一個垂直的LinearLayout,垂直的LinearLayout里包含兩個TextView。以下是這個布局的源代碼:
<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>
如果你將它作為ListView的item,它能正常工作,但卻是相當(dāng)浪費(fèi)的。相同的布局可以使用RelativeLayout進(jìn)行重寫,相對于每個列表項(xiàng)來說,可以節(jié)省一個View,且View層級上更好,只有一層。使用RelativeLayout也很簡單:
<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>
新的實(shí)現(xiàn)與老的實(shí)現(xiàn)看起來效果完全一致,除了一種情況。每個列表項(xiàng)顯示兩行文字:標(biāo)題和可選的描述。當(dāng)某一個列表項(xiàng)的描述不可獲得時(shí),應(yīng)用程序可能希望將第二個TextView的Visibility設(shè)為GONE。LinearLayout實(shí)現(xiàn)版表現(xiàn)得很完美,但RelativeLayout實(shí)現(xiàn)版就有點(diǎn)差強(qiáng)人意了:
在RelativeLayout里,每個View都是和父元素RelativeLayout對齊或是和其它View對齊的。例如,我們聲明描述部分是和RelativeLayout的底部對齊,標(biāo)題位于其上并與RelativeLayout的頂端對齊。當(dāng)描述GONE時(shí),RelativeLayout不知道怎么去放置標(biāo)題的底邊緣。為了解決這個問題,你可以使用一個非常簡單的布局參數(shù):layout_alignWithParentIfMissing。
這個布爾參數(shù)告訴RelativeLayout:如果目標(biāo)對象消失時(shí)使用自己的邊緣作為錨點(diǎn)。例如,如果你放置一個View到另一個Visibiity屬性設(shè)為GONE的View的右邊,且設(shè)定alignWithParentIfMissing為true,RelativeLayout就會將其左邊緣作為View的對齊錨點(diǎn)。在我們的這個場合,使用alignWithParentIfMissing的結(jié)果是RelativeLayout將標(biāo)題部分的底部與自己的底部對齊。結(jié)果如下所示:
現(xiàn)在,我們的布局表現(xiàn)得很完美了,即使描述部分的Visibility屬性設(shè)為GONE。更好的是,層級更加簡單,因?yàn)槲覀儾辉偈褂肔inearLayout,而且,更加高效了。當(dāng)我們使用HierarchyViewer來比較兩個實(shí)現(xiàn)版的時(shí)候,事實(shí)就更明顯了:
另外,當(dāng)你使用這么一個布局作為ListView的列表項(xiàng)時(shí),這種差異就更為重要了。希望這個簡單的例子能讓你了解布局,了解如何優(yōu)化你的UI。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 5.0中CoordinatorLayout的使用技巧
- CoordinatorLayout的使用如此簡單(Android)
- Android 使用CoordinatorLayout實(shí)現(xiàn)滾動標(biāo)題欄效果的實(shí)例
- Android CoordinatorLayout詳解及實(shí)例代碼
- Android Studio使用小技巧:布局預(yù)覽時(shí)填充數(shù)據(jù)
- Android中ListView Item布局優(yōu)化技巧
- Android布局技巧之合并布局
- Android布局技巧之創(chuàng)建可重用的UI組件
- Android中關(guān)于CoordinatorLayout的一些實(shí)用布局技巧
相關(guān)文章
Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果
這篇文章主要介紹了Android基于google Zxing實(shí)現(xiàn)各類二維碼掃描效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02基于Android 錯誤信息捕獲發(fā)送至服務(wù)器的詳解
本篇文章是對Android中錯誤信息捕獲發(fā)送服務(wù)器進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android中SeekBar和RatingBar用法實(shí)例分析
這篇文章主要介紹了Android中SeekBar和RatingBar用法,結(jié)合實(shí)例形式分析了SeekBar和RatingBar的功能、定義與簡單使用方法,需要的朋友可以參考下2016-06-06微信小程序 canvas開發(fā)實(shí)例及注意事項(xiàng)
這篇文章主要介紹了微信小程序 wxcanvas開發(fā)實(shí)例及注意事項(xiàng)的相關(guān)資料,這里對微信canvas與H5中的canvas做對比,并說明注意事項(xiàng),需要的朋友可以參考下2016-12-12Android代碼檢查規(guī)則Lint的自定義與應(yīng)用詳解
本文主要介紹了Android代碼檢查規(guī)則Lint的自定義與應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android通過Service實(shí)現(xiàn)簡單的音樂播放
這篇文章主要介紹了Android通過Service實(shí)現(xiàn)簡單的音樂播放,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android實(shí)現(xiàn)列表時(shí)間軸
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)列表時(shí)間軸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05[Alibaba-ARouter]淺談簡單好用的Android頁面路由框架
這篇文章主要介紹了[Alibaba-ARouter]淺談簡單好用的Android頁面路由框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11