Android中View繪製最佳化二一—- 使用標籤複用布局檔案

來源:互聯網
上載者:User

                                                                          
 本文原創, 轉載請註明出處:http://blog.csdn.net/qinjuning 
 譯二:

                                          使用<include />標籤複用布局檔案

  翻譯地址:http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge



           儘管Android通過內建了各種各樣的控制項提供了微小、可複用的互動性元素,也許你需要複用較大的

     組件 ---- 某些特定布局檔案 。為了更有效率複用的布局檔案,你可以使用<include />以及<merge />     標籤將其他的布局檔案加入到當前的布局檔案中。
          複用布局檔案是一種特彆強大的方法,它允許你建立可複用性的布局檔案。例如,一個包含“Yse”or“No”的     Button面版,或者是帶有文字說明的 Progressbar。複用布局檔案同樣意味著你應用程式裡的任何元素都能從     繁雜的布局檔案提取出來進行單獨管理,接著你需要做的只是加入這些獨立的布局檔案(因為他們都是可複用地)。     因此,當你通過自訂View建立獨立的UI組件時,你可以複用布局檔案讓事情變得更簡單。

 1、建立一個可複用性的布局檔案
        如果你已經知道複用布局的”面貌”,那麼建立、定義布局檔案( 命名以”.xml”為尾碼)。例如,這裡是一個來自 G- Kenya
codelab 的布局檔案,定義了在每個Activity中都要使用的一個自訂標題 (titlebar.xml):由於這些  可複用性布局被添加至其他布局檔案中,因此,它的每個根視圖(root View)最好是精確(exactly)的。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width=”match_parent”    android:layout_height="wrap_content"    android:background="@color/titlebar_bg">    <ImageView android:layout_width="wrap_content"               android:layout_height="wrap_content"                android:src="@drawable/gafricalogo" /></FrameLayout>
 2、使用<include />標籤

          在需要添加這些布局的地方,使用<include />標籤 。 例如,下面是一個來自G-Kenya codelab的布局檔案,

  它複用了上面列出的“title bar”檔案, 該布局檔案如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"     android:layout_width=”match_parent”    android:layout_height=”match_parent”    android:background="@color/app_bg"    android:gravity="center_horizontal">    <include layout="@layout/titlebar"/>    <TextView android:layout_width=”match_parent”              android:layout_height="wrap_content"              android:text="@string/hello"              android:padding="10dp" />    ...</LinearLayout>

                 你也可以在<include />節點中為被添加的布局檔案的root View定義特別標識,重寫所有layout參數即可(任何  以“android:layout_”為首碼的屬性)。例如:

<include android:id=”@+id/news_title”         android:layout_width=”match_parent”         android:layout_height=”match_parent”         layout=”@layout/title”/>

 3、使用<merge />標籤

        當在布局檔案中複用另外的布局時, <merge />標籤能夠在布局層次消除多餘的視圖元素。例如,如果你的

   主布局檔案是一個垂直地包含兩個View的LinearLayout,該布局能夠複用在其他布局中,而對任意包含兩個View的   布局檔案都需要一個root View(否則, 編譯器會提示錯誤)。然而,在該可複用性布局中添加一個LinearLayout   作為root View,將會導致一個垂直的LinearLayout包含另外的垂直LinearLayout。內嵌地LinearLayout只能減緩   UI效率,其他毫無用處可言。            該複用性布局利用.xml呈現如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical"         android:layout_width=”match_parent”        android:layout_height=”match_parent”        android:background="@color/app_bg"        android:gravity="horizontal">    <Button        android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="@string/add"/>    <Button        android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="@string/delete"/></LinearLayout>

      為了避免冗餘的布局元素,你可以使用<merge />作為複用性布局檔案地root View 。例如:
           使用<merge />標籤的布局檔案:

<merge xmlns:android="http://schemas.android.com/apk/res/android">    <Button        android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="@string/add"/>    <Button        android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="@string/delete"/></merge>

         現在,當你添加該布局檔案時(使用<include />標籤),系統忽略< merge />節點並且直接添加兩個Button去  取代<include />節點。


   另外的,按需載入View視圖 ,請看:

                http://developer.android.com/training/improving-layouts/loading-ondemand.html

   如何使ListView流暢滑動 ,請看:

                http://developer.android.com/training/improving-layouts/smooth-scrolling.html




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.