日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android 布局優(yōu)化

布局原則

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

創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設與策劃設計,永仁網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十多年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:永仁等地區(qū)。永仁做網(wǎng)站價格咨詢:028-86922220

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

將可復用的組件抽取出來并通過include標簽使用;

使用ViewStub標簽來加載一些不常用的布局;

使用merge標簽減少布局的嵌套層次;

RelativeLayout VS LinearLayout

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

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

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

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

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

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

查看布局層次

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

通過布局圖我們可以看到根節(jié)點DecorView下 包含一個LinearLayout, 這個LinearLayout就是包含Activity布局和狀態(tài)欄的整個屏幕顯示的布局父節(jié)點,這個LinearLayout有兩個子節(jié)點, 一個是FrameLayout, FrameLayout就是Activity布局中默認的父布局節(jié)點, 這個節(jié)點下面就包含了我們自己寫的xml布局, 還有一個子節(jié)點就是ViewStub,關于這個節(jié)點我們在后面會詳細介紹。

< include />的使用

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

header.xml文件

 
 
 
 
  1.     android:layout_width="match_parent" 
  2.     android:layout_height="match_parent" > 
  3.  
  4.     
  5.         android:id="@+id/button" 
  6.         android:layout_width="match_parent" 
  7.         android:layout_height="@dimen/dp_40" 
  8.         android:layout_above="@+id/text"/> 
  9.  
  10.     
  11.         android:id="@+id/text" 
  12.         android:layout_width="match_parent" 
  13.         android:layout_height="@dimen/dp_40" 
  14.         android:layout_alignParentBottom="true" 
  15.         android:text="@string/app_name" /> 

然后我們在需要引入footer的布局xml中通過include導入這個共用布局。

main.xml文件

 
 
 
 
  1.     android:layout_width="match_parent" 
  2.     android:layout_height="match_parent"> 
  3.  
  4.     
  5.         android:layout_width="match_parent" 
  6.         android:layout_height="wrap_content" 
  7.         android:text="hello world" /> 
  8.  
  9.     
  10.         android:layout_width="match_parent" 
  11.         android:layout_height="match_parent" 
  12.         android:layout_gravity="center" > 
  13.  
  14.          
  15.  
  16.      

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

< merge />的使用

merge標簽的作用是合并UI布局,使用該標簽能降低UI布局的嵌套層次。merge標簽可用于兩種典型情況:

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

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

以***種情況為例,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.     
  6.         android:layout_width="match_parent" 
  7.         android:layout_height="match_parent"> 
  8.  
  9.         
  10.             android:layout_width="match_parent" 
  11.             android:layout_height="wrap_content" 
  12.             android:text="hello world" /> 
  13.  
  14.         
  15.             android:layout_width="match_parent" 
  16.             android:layout_height="match_parent" 
  17.             android:layout_gravity="center" > 
  18.  
  19.              
  20.  
  21.          
  22.      

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

 
 
 
 
  1.     android:layout_width="match_parent" 
  2.     android:layout_height="match_parent" > 
  3.   
  4.     
  5.         android:id="@+id/button" 
  6.         android:layout_width="match_parent" 
  7.         android:layout_height="@dimen/dp_40" 
  8.         android:layout_above="@+id/text"/> 
  9.   
  10.     
  11.         android:id="@+id/text" 
  12.         android:layout_width="match_parent" 
  13.         android:layout_height="@dimen/dp_40" 
  14.         android:layout_alignParentBottom="true" 
  15.         android:text="@string/app_name" /> 
  16.   

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

ViewStub標簽

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

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

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

然后在main.xml里面加入ViewStub的標簽引入上面的布局:

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

在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,同時我們對errorView進行了保存,這樣下次不用繼續(xù)inflate。

總結

這篇Blog沒有詳細介紹 HierarchyViewer工具的使用,相信如果對布局原則比較熟練之后,對工具的依賴大大減少,開發(fā)效率也會大大的提升。除這些布局原則之外,還需 要大家對Android各個組件的屬性很熟悉,比如如果要做這么一個布局, 一個圖片和一個文本的布局,新手們往往會用一個Layout嵌套ImageView和TextView來做, 但是當我們知道TextView有drawableLeft, drawableRight等屬性時,那么實現(xiàn)這樣的一個布局是非??焖俑咝У?。總之,且學且實踐!


分享名稱:Android 布局優(yōu)化
文章路徑:http://m.5511xx.com/article/codicdd.html