Android開發最佳化之——對介面UI的最佳化(1)

來源:互聯網
上載者:User

在Android應用開發過程中,螢幕上控制項的布局代碼和程式的邏輯代碼通常是分開的。介面的布局代碼是放在一個獨立的xml檔案中的,這個檔案裡面是樹型組織的,控制著頁面的布局。通常,在這個頁面中會用到很多控制項,控制項會用到很多的資源。Android系統本身有很多的資源,包括各種各樣的字串、圖片、動畫、樣式和布局等等,這些都可以在應用程式中直接使用。這樣做的好處很多,既可以減少記憶體的使用,又可以減少部分工作量,也可以縮減程式安裝包的大小。

下面從幾個方面來介紹如何利用系統資源。

1)利用系統定義的id

比如我們有一個定義ListView的xml檔案,一般的,我們會寫類似下面的程式碼片段。

 
  1. <ListView 
  2.  
  3.     android:id="@+id/mylist" 
  4.  
  5.     android:layout_width="fill_parent" 
  6.  
  7.     android:layout_height="fill_parent"/> 
  8.   

這裡我們定義了一個ListView,定義它的id是"@+id/mylist"。實際上,如果沒有特別的需求,就可以利用系統定義的id,類似下面的樣子。

 
  1. <ListView 
  2.  
  3.     android:id="@android:id/list" 
  4.  
  5.     android:layout_width="fill_parent" 
  6.  
  7.     android:layout_height="fill_parent"/> 
  8.   

在xml檔案中引用系統的id,只需要加上“@android:”首碼即可。如果是在Java代碼中使用系統資源,和使用自己的資源基本上是一樣的。不同的是,需要使用android.R類來使用系統的資源,而不是使用應用程式指定的R類。這裡如果要擷取ListView可以使用android.R.id.list來擷取。

2)利用系統的圖片資源

假設我們在應用程式中定義了一個menu,xml檔案如下。

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <menu xmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5.     <item 
  6.  
  7.         android:id="@+id/menu_attachment" 
  8.  
  9.         android:title="附件" 
  10.  
  11.         android:icon="@android:drawable/ic_menu_attachment" /> 
  12.  
  13. </menu> 
  14.   

其中程式碼片段android:icon="@android:drawable/ic_menu_attachment"本來是想引用系統中已有的Menu裡的“附件”的表徵圖。但是在Build工程以後,發現出現了錯誤。提示資訊如下:

 
  1. error: Error: Resource is not public. (at 'icon' with value '@android:drawable/ic_menu_attachment'). 
  2.   

從錯誤的提示資訊大概可以看出,由於該資源沒有被公開,所以無法在我們的應用中直接引用。既然這樣的話,我們就可以在Android SDK中找到相應的圖片資源,直接拷貝到我們的工程目錄中,然後使用類似android:icon="@drawable/ic_menu_attachment"的程式碼片段進行引用。

這樣做的好處,一個是美工不需要重複的做一份已有的圖片了,可以節約不少工時;另一個是能保證我們的應用程式的風格與系統一致。

經驗分享:

Android中沒有公開的資源,在xml中直接引用會報錯。除了去找到對應資源並拷貝到我們自己的應用目錄下使用以外,我們還可以將引用“@android”改成“@*android”解決。比如上面引用的附件表徵圖,可以修改成下面的代碼。

android:icon="@*android:drawable/ic_menu_attachment"

修改後,再次Build工程,就不會報錯了。

3)利用系統的字串資源

假設我們要實現一個Dialog,Dialog上面有“確定”和“取消”按鈕。就可以使用下面的代碼直接使用Android系統內建的字串。

 
  1. <LinearLayout 
  2.  
  3.        android:orientation="horizontal" 
  4.  
  5.        android:layout_width="fill_parent"  
  6.  
  7.        android:layout_height="wrap_content"> 
  8.  
  9.        <Button 
  10.  
  11.            android:id="@+id/yes"  
  12.  
  13.            android:layout_width="fill_parent" 
  14.  
  15.            android:layout_height="wrap_content" 
  16.  
  17.            android:layout_weight="1.0" 
  18.  
  19.            android:text="@android:string/yes"/> 
  20.  
  21.        <Button 
  22.  
  23.            android:id="@+id/no"  
  24.  
  25.            android:layout_width="fill_parent" 
  26.  
  27.            android:layout_height="wrap_content" 
  28.  
  29.            android:layout_weight="1.0" 
  30.  
  31.            android:text="@android:string/no"/> 
  32.  
  33.    </LinearLayout> 

如果使用系統的字串,預設就已經支援多語言環境了。如上述代碼,直接使用了@android:string/yes和@android:string/no,在簡體中文環境下會顯示“確定”和“取消”,在英文環境下會顯示“OK”和“Cancel”。

4)利用系統的Style

假設布局檔案中有一個TextView,用來顯示視窗的標題,使用中等大小字型。可以使用下面的程式碼片段來定義TextView的Style。

 
  1. <TextView 
  2.  
  3.         android:id="@+id/title" 
  4.  
  5.         android:layout_width="wrap_content" 
  6.  
  7.         android:layout_height="wrap_content" 
  8.  
  9.         android:textAppearance="?android:attr/textAppearanceMedium" /> 
  10.   
  11.  
  12.   

其中android:textAppearance="?android:attr/textAppearanceMedium"就是使用系統的style。需要注意的是,使用系統的style,需要在想要使用的資源前面加“?android:”作為首碼,而不是“@android:”。

5)利用系統的顏色定義

除了上述的各種系統資源以外,還可以使用系統定義好的顏色。在項目中最常用的,就是透明色的使用。程式碼片段如下。

 
  1. android:background ="@android:color/transparent" 
  2.   

經驗分享:

Android系統本身有很多資源在應用中都可以直接使用,具體的,可以進入android-sdk的相應檔案夾中去查看。例如:可以進入$android-sdk$\platforms\android-8\data\res,裡面的系統資源就一覽無餘了。

開發人員需要花一些時間去熟悉這些資源,特別是圖片資源和各種Style資源,這樣在開發過程中,能夠想到有相關資源並且直接拿來使用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.