Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 解析Android開發優化之:對界面UI的優化詳解(一)

解析Android開發優化之:對界面UI的優化詳解(一)

編輯:關於Android編程

通常,在這個頁面中會用到很多控件,控件會用到很多的資源。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資源,這樣在開發過程中,能夠想到有相關資源並且直接拿來使用。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved