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

轉自:http://blog.csdn.net/arui319/article/details/8549849


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

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

 

1)利用系統定義的id

比如我們有一個定義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)利用系統的圖片資源

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

<?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工程以後,發現出現了錯誤。提示信息如下:

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

 

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

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

 

經驗分享:

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資源,這樣在開發過程中,能夠想到有相關資源並且直接拿來使用。

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章