Android開發優化之——對界面UI的優化(1)

在Android應用開發過程當中,屏幕上控件的佈局代碼和程序的邏輯代碼一般是分開的。界面的佈局代碼是放在一個獨立的xml文件中的,這個文件裏面是樹型組織的,控制着頁面的佈局。一般,在這個頁面中會用到不少控件,控件會用到不少的資源。Android系統自己有不少的資源,包括各類各樣的字符串、圖片、動畫、樣式和佈局等等,這些均可以在應用程序中直接使用。這樣作的好處不少,既能夠減小內存的使用,又能夠減小部分工做量,也能夠縮減程序安裝包的大小。java

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

1)利用系統定義的idweb

好比咱們有一個定義ListView的xml文件,通常的,咱們會寫相似下面的代碼片斷。佈局

<ListView
    android:id="@+id/mylist"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

這裏咱們定義了一個ListView,定義它的id是"@+id/mylist"。實際上,若是沒有特別的需求,就能夠利用系統定義的id,相似下面的樣子。字體

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

在xml文件中引用系統的id,只須要加上@android :」前綴便可。若是是在Java代碼中使用系統資源,和使用本身的資源基本上是同樣的。不一樣的是,須要使用android.R類來使用系統的資源,而不是使用應用程序指定的R類。這裏若是要獲取ListView可使用android.R.id.list來獲取。動畫

2)利用系統的圖片資源ui

   假設咱們在應用程序中定義了一個menu,xml文件以下。spa

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_attachment"
        android:title="附件"
        android:icon="@android:drawable/ic_menu_attachment" />
</menu>

其中代碼片斷android:icon="@android :drawable/ic_menu_attachment"原本是想引用系統中已有的Menu裏的「附件」的圖標。可是在Build工程之後,發現出現了錯誤。提示信息以下:.net

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

從錯誤的提示信息大概能夠看出,因爲該資源沒有被公開,因此沒法在咱們的應用中直接引用。既然這樣的話,咱們就能夠在Android SDK中找到相應的圖片資源,直接拷貝到咱們的工程目錄中,而後使用相似android:icon="@drawable/ic_menu_attachment"的代碼片斷進行引用。code

這樣作的好處,一個是美工不須要重複的作一份已有的圖片了,能夠節約很多工時;另外一個是能保證咱們的應用程序的風格與系統一致。


經驗分享:

Android中沒有公開的資源,在xml中直接引用會報錯。除了去找到對應資源並拷貝到咱們本身的應用目錄下使用之外,咱們還能夠將引用「@android」改爲「@*android」解決。好比上面引用的附件圖標,能夠修改爲下面的代碼。

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

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


3)利用系統的字符串資源

   假設咱們要實現一個Dialog,Dialog上面有「肯定」和「取消」按鈕。就可使用下面的代碼直接使用Android系統自帶的字符串。

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/yes" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="@android:string/yes"/>
        <Button
            android:id="@+id/no" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="@android:string/no"/>
    </LinearLayout>

若是使用系統的字符串,默認就已經支持多語言環境了。如上述代碼,直接使用了@android:string/yes和@android:string/no,在簡體中文環境下會顯示「肯定」和「取消」,在英文環境下會顯示「OK」和「Cancel」。

4)利用系統的Style

   假設佈局文件中有一個TextView,用來顯示窗口的標題,使用中等大小字體。可使用下面的代碼片斷來定義TextView的Style。

 <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

其中android:textAppearance="?android:attr/textAppearanceMedium"就是使用系統的style。須要注意的是,使用系統的style,須要在想要使用的資源前面加「?android:」做爲前綴,而不是「@android:」。

 

5)利用系統的顏色定義

除了上述的各類系統資源之外,還可使用系統定義好的顏色。在項目中最經常使用的,就是透明色的使用。代碼片斷以下。

android:background ="@android:color/transparent"

 

經驗分享:

Android系統自己有不少資源在應用中均可以直接使用,具體的,能夠進入android-sdk的相應文件夾中去查看。例如:能夠進入$android-sdk$\platforms\android-8\data\res,裏面的系統資源就盡收眼底了。

開發者須要花一些時間去熟悉這些資源,特別是圖片資源和各類Style資源,這樣在開發過程當中,可以想到有相關資源而且直接拿來使用。

---------------------------------------------------------------------------

http://blog.csdn.net/arui319

《Android應用開發精解》已出版,本文是初稿的部份內容。歡迎購買閱讀。

本文能夠轉載,可是請保留以上做者信息。

謝謝。

---------------------------------------------------------------------------

相關文章
相關標籤/搜索