自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android 布局優(yōu)化

移動(dòng)開發(fā) Android
在開發(fā)過程中我們經(jīng)常說性能優(yōu)化,但性能優(yōu)化是一個(gè)比較寬泛的概念。在Android開發(fā)中性能優(yōu)化可能包括:Java代碼優(yōu)化, 算法優(yōu)化, SQLite優(yōu)化, 布局優(yōu)化等。那么這篇博客就來總結(jié)并分享下Android開發(fā)中的布局優(yōu)化。

布局原則

在Android UI布局過程中,通過遵守一些慣用、有效的布局原則,我們可以制作出高效且復(fù)用性高的UI,概括來說包括如下幾點(diǎn):

盡量多使用RelativeLayout 和LinearLayout, 不要使用絕對(duì)布局AbsoluteLayout,在布局層次一樣的情況下, 建議使用LinearLayout代替RelativeLayout, 因?yàn)長inearLayout性能要稍高一點(diǎn),但往往RelativeLayout可以簡單實(shí)現(xiàn)LinearLayout嵌套才能實(shí)現(xiàn)的布局。

將可復(fù)用的組件抽取出來并通過include標(biāo)簽使用;

使用ViewStub標(biāo)簽來加載一些不常用的布局;

使用merge標(biāo)簽減少布局的嵌套層次;

RelativeLayout VS LinearLayout

***條原則說了布局層次一樣的情況下LinearLayout比RelativeLayout要好, 但往往RelativeLayout可以簡單實(shí)現(xiàn)LinearLayout嵌套才能實(shí)現(xiàn)的布局。假如需要實(shí)現(xiàn)如下布局:

用LinearLayout來實(shí)現(xiàn)xml代碼如下:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="?android:attr/listPreferredItemHeight" 
  4.     android:padding="6dip"
  5.      
  6.     <ImageView 
  7.         android:id="@+id/icon" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="fill_parent" 
  10.         android:layout_marginRight="6dip" 
  11.         android:src="@drawable/icon" /> 
  12.  
  13.     <LinearLayout 
  14.         android:orientation="vertical" 
  15.         android:layout_width="0dip" 
  16.         android:layout_weight="1" 
  17.         android:layout_height="fill_parent"
  18.  
  19.         <TextView 
  20.             android:layout_width="fill_parent" 
  21.             android:layout_height="0dip" 
  22.             android:layout_weight="1" 
  23.             android:gravity="center_vertical" 
  24.             android:text="My Application" /> 
  25.              
  26.         <TextView   
  27.             android:layout_width="fill_parent" 
  28.             android:layout_height="0dip" 
  29.             android:layout_weight="1"  
  30.             android:singleLine="true" 
  31.             android:ellipsize="marquee" 
  32.             android:text="Simple application that shows how to use RelativeLayout" /> 
  33.              
  34.     </LinearLayout></LinearLayout> 

而用RelativeLayout實(shí)現(xiàn)代碼如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="?android:attr/listPreferredItemHeight" 
  4.     android:padding="6dip"
  5.      
  6.     <ImageView 
  7.         android:id="@+id/icon" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="fill_parent" 
  10.         android:layout_alignParentTop="true" 
  11.         android:layout_alignParentBottom="true" 
  12.         android:layout_marginRight="6dip" 
  13.         android:src="@drawable/icon" /> 
  14.  
  15.     <TextView   
  16.         android:id="@+id/secondLine" 
  17.         android:layout_width="fill_parent" 
  18.         android:layout_height="26dip"  
  19.         android:layout_toRightOf="@id/icon" 
  20.         android:layout_alignParentBottom="true" 
  21.         android:layout_alignParentRight="true" 
  22.         android:singleLine="true" 
  23.         android:ellipsize="marquee" 
  24.         android:text="Simple application that shows how to use RelativeLayout" /> 
  25.  
  26.     <TextView 
  27.         android:layout_width="fill_parent" 
  28.         android:layout_height="wrap_content" 
  29.         android:layout_toRightOf="@id/icon" 
  30.         android:layout_alignParentRight="true" 
  31.         android:layout_alignParentTop="true" 
  32.         android:layout_above="@id/secondLine" 
  33.         android:layout_alignWithParentIfMissing="true" 
  34.         android:gravity="center_vertical" 
  35.         android:text="My Application" /></RelativeLayout> 

可以看到用RelativeLayout實(shí)現(xiàn),布局層次明顯少了,所以大多數(shù)時(shí)候優(yōu)先推薦使用RelativeLayout。

 

查看布局層次

如何查看布局層次呢?有兩種辦法:一是通過手機(jī)的開 發(fā)者選項(xiàng),4.0及以上Android版本可通過設(shè)置->開發(fā)者選項(xiàng)->顯示布局邊界打開頁面布局顯示,看看是否有不必要的節(jié)點(diǎn)和嵌套。第二 種就是利用SDK自帶的UI性能檢測工具HierarchyViewer。 進(jìn)入sdk目錄下的tools文件夾下,找到HierarchyViewer并運(yùn)行(此時(shí)保持你的模擬器或真機(jī)正在運(yùn)行需要進(jìn)行分析的App),雙擊我們 正在顯示的這個(gè)App所代表的進(jìn)程。接下來便會(huì)進(jìn)入hierarchyviewer的界面,我們可以在這里很清晰看到正在運(yùn)行的UI的布局層次結(jié)構(gòu)以及它 們之間的關(guān)系。大概的顯示如下圖:

通過布局圖我們可以看到根節(jié)點(diǎn)DecorView下 包含一個(gè)LinearLayout, 這個(gè)LinearLayout就是包含Activity布局和狀態(tài)欄的整個(gè)屏幕顯示的布局父節(jié)點(diǎn),這個(gè)LinearLayout有兩個(gè)子節(jié)點(diǎn), 一個(gè)是FrameLayout, FrameLayout就是Activity布局中默認(rèn)的父布局節(jié)點(diǎn), 這個(gè)節(jié)點(diǎn)下面就包含了我們自己寫的xml布局, 還有一個(gè)子節(jié)點(diǎn)就是ViewStub,關(guān)于這個(gè)節(jié)點(diǎn)我們?cè)诤竺鏁?huì)詳細(xì)介紹。

 

< include />的使用

在實(shí)際開發(fā)中,我們經(jīng)常會(huì)遇到一些共用的UI組件, 比如帶返回按鈕的導(dǎo)航欄,如果為每一個(gè)xml文件都設(shè)置這部分布局,一是重復(fù)的工作量大,二是如果有變更,那么每一個(gè)xml文件都得修改。還 好,Android為我們提供了include標(biāo)簽,顧名思義,通過它,我們可以將這些共用的組件抽取出來單獨(dú)放到一個(gè)xml文件中,然后使用 include標(biāo)簽導(dǎo)入共用布局,這樣,前面提到的兩個(gè)問題都解決了。下面以在一個(gè)布局main.xml中用include引入另一個(gè)布局 header.xml為例。

header.xml文件

  1. <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" > 
  4.  
  5.     <Button 
  6.         android:id="@+id/button" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="@dimen/dp_40" 
  9.         android:layout_above="@+id/text"/> 
  10.  
  11.     <TextView 
  12.         android:id="@+id/text" 
  13.         android:layout_width="match_parent" 
  14.         android:layout_height="@dimen/dp_40" 
  15.         android:layout_alignParentBottom="true" 
  16.         android:text="@string/app_name" /></RelativeLayout> 

然后我們?cè)谛枰雈ooter的布局xml中通過include導(dǎo)入這個(gè)共用布局。

main.xml文件

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent"
  4.  
  5.     <TextView 
  6.         android:layout_width="match_parent" 
  7.         android:layout_height="wrap_content" 
  8.         android:text="hello world" /> 
  9.  
  10.     <RelativeLayout  
  11.         android:layout_width="match_parent" 
  12.         android:layout_height="match_parent" 
  13.         android:layout_gravity="center" > 
  14.  
  15.         <include layout="@layout/header" /> 
  16.  
  17.     </RelativeLayout></FrameLayout> 

通過這種方式,我們既能提高UI的制作和復(fù)用效率,也能保證制作的UI布局更加規(guī)整和易維護(hù)。

 

< merge />的使用

merge標(biāo)簽的作用是合并UI布局,使用該標(biāo)簽?zāi)芙档蚒I布局的嵌套層次。merge標(biāo)簽可用于兩種典型情況:

  • 布局根結(jié)點(diǎn)是FrameLayout且不需要設(shè)置background或padding等屬性,可以用merge代替,因?yàn)锳ctivity內(nèi)容布局的parent view就是個(gè)FrameLayout,所以可以用merge消除只剩一個(gè),這一點(diǎn)可以從上圖中看到。

  • 某布局作為子布局被其他布局include時(shí),使用merge當(dāng)作該布局的頂節(jié)點(diǎn),這樣在被引入時(shí)頂結(jié)點(diǎn)會(huì)自動(dòng)被忽略,而將其子節(jié)點(diǎn)全部合并到主布局中。

以***種情況為例,main.xml布局就可以優(yōu)化如下:

  1. merge xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent"
  4.  
  5.     <FrameLayout  
  6.         android:layout_width="match_parent" 
  7.         android:layout_height="match_parent"
  8.  
  9.         <TextView 
  10.             android:layout_width="match_parent" 
  11.             android:layout_height="wrap_content" 
  12.             android:text="hello world" /> 
  13.  
  14.         <RelativeLayout  
  15.             android:layout_width="match_parent" 
  16.             android:layout_height="match_parent" 
  17.             android:layout_gravity="center" > 
  18.  
  19.             <include layout="@layout/header" /> 
  20.  
  21.         </RelativeLayout> 
  22.     </FrameLayout></merge> 

以第二種情況為例,header.xml布局可以優(yōu)化如下:

  1. <?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" > 
  4.   
  5.     <Button 
  6.         android:id="@+id/button" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="@dimen/dp_40" 
  9.         android:layout_above="@+id/text"/> 
  10.   
  11.     <TextView 
  12.         android:id="@+id/text" 
  13.         android:layout_width="match_parent" 
  14.         android:layout_height="@dimen/dp_40" 
  15.         android:layout_alignParentBottom="true" 
  16.         android:text="@string/app_name" /> 
  17.  </merge> 

這樣就不會(huì)有多余的FrameLayout和RelativeLayout節(jié)點(diǎn)了。

 

ViewStub標(biāo)簽

viewstub標(biāo)簽同include標(biāo)簽一樣可以 用來引入一個(gè)外部布局,不同的是,viewstub引入的布局默認(rèn)不會(huì)擴(kuò)張,即既不會(huì)占用顯示也不會(huì)占用位置,從而在解析layout時(shí)節(jié)省cpu和內(nèi) 存。 viewstub常用來引入那些默認(rèn)不會(huì)顯示,只在特殊情況下顯示的布局,如進(jìn)度布局、網(wǎng)絡(luò)失敗顯示的刷新布局、信息出錯(cuò)出現(xiàn)的提示布局等。

我們新建一個(gè)xml文件用來顯示一個(gè)網(wǎng)絡(luò)錯(cuò)誤時(shí)提示信息error.xml:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" > 
  5.  
  6.    <TextView 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_centerInParent="true" 
  10.         android:background="@android:color/white" 
  11.         android:padding="10dip" 
  12.         android:text="Message" 
  13.         android:textColor="@android:color/black" /></RelativeLayout 

然后在main.xml里面加入ViewStub的標(biāo)簽引入上面的布局:

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:background="@android:color/darker_gray" 
  5.     android:layout_height="match_parent" > 
  6.  
  7.     ...    <ViewStub 
  8.         android:id="@+id/error_layout" 
  9.         android:layout_width="wrap_content" 
  10.         android:layout_height="wrap_content" 
  11.         android:layout_gravity="center" 
  12.         android:layout="@layout/error" /></merge> 

在java中通過(ViewStub)findViewById(id)找到ViewStub,通過stub.inflate()展開ViewStub,然后得到子View,如下:

  1. private View errorView; 
  2.  private void showError() { 
  3.     // not repeated infalte 
  4.     if (errorView != null) { 
  5.         errorView.setVisibility(View.VISIBLE); 
  6.         return
  7.     } 
  8.   
  9.     ViewStub stub = (ViewStub)findViewById(R.id.error_layout); 
  10.     errorView = stub.inflate();} 
  11.  private void showContent() { 
  12.     if (errorView != null) { 
  13.         errorView.setVisibility(View.GONE); 
  14.     }} 

在上面showError()中展開了ViewStub,同時(shí)我們對(duì)errorView進(jìn)行了保存,這樣下次不用繼續(xù)inflate。

 

總結(jié)

這篇Blog沒有詳細(xì)介紹 HierarchyViewer工具的使用,相信如果對(duì)布局原則比較熟練之后,對(duì)工具的依賴大大減少,開發(fā)效率也會(huì)大大的提升。除這些布局原則之外,還需 要大家對(duì)Android各個(gè)組件的屬性很熟悉,比如如果要做這么一個(gè)布局, 一個(gè)圖片和一個(gè)文本的布局,新手們往往會(huì)用一個(gè)Layout嵌套ImageView和TextView來做, 但是當(dāng)我們知道TextView有drawableLeft, drawableRight等屬性時(shí),那么實(shí)現(xiàn)這樣的一個(gè)布局是非常快速高效的??傊覍W(xué)且實(shí)踐!

責(zé)任編輯:chenqingxiang 來源: oschina
相關(guān)推薦

2012-05-08 16:37:23

android

2013-09-17 10:17:39

Android布局

2011-03-24 09:03:13

Android SDKAndroid

2023-08-25 08:06:04

項(xiàng)目布局LazyRow?

2009-12-31 15:21:48

Silverlight

2017-05-11 15:01:43

Androidweb布局

2011-05-31 09:36:46

Android 布局屬性

2021-07-29 14:20:34

網(wǎng)絡(luò)優(yōu)化移動(dòng)互聯(lián)網(wǎng)數(shù)據(jù)存儲(chǔ)

2011-04-07 08:59:47

Android交互設(shè)計(jì)

2016-09-22 09:24:33

AndroidViewStub

2014-09-05 10:10:32

Android自適應(yīng)布局設(shè)計(jì)

2019-07-25 13:22:43

AndroidAPK文件優(yōu)化

2019-12-13 10:25:08

Android性能優(yōu)化啟動(dòng)優(yōu)化

2013-09-16 15:16:20

Android性能優(yōu)化

2013-03-27 09:17:17

Android開發(fā)AndroidList

2013-09-17 10:32:08

Android性能優(yōu)化數(shù)據(jù)庫

2011-04-19 09:19:37

相對(duì)布局界面設(shè)計(jì)Android

2011-04-11 17:25:30

線性布局用戶界面設(shè)計(jì)Android

2010-10-14 08:55:02

Android布局方式

2011-04-11 17:07:33

布局基礎(chǔ)用戶界面設(shè)計(jì)Android
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)