在android布局中使用include和merge標簽

pxdb 9年前發布 | 16K 次閱讀 Android Android開發 移動開發

在我們開發android布局時,經常會有很多的布局是相同的,這個時候我們可以通過<include/>和<merge/>標簽實現將復雜的布局包含在需要的布局中,減少重復代碼的編寫。



  1. 創建一個可以重復使用的布局:

    如下代碼描述在應用中每個acitivity都出現的頂欄titlebar.xml


        <FrameLayout xmlns:android="
    
    
     android:layout_width=”match_parent”  
     android:layout_height="wrap_content"  
     android:background="@color/titlebar_bg">  
    
     <ImageView android:id="@+id/title"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"   
                android:src="@drawable/gafricalogo" />  
    

    </FrameLayout> </pre>


    上面的根布局(root view)即frameLayout會出現在之后插入的地方。

    2. 使用<include/>標簽:

    在應用中的一個activity的布局中頂欄就是如上的布局,那么我們就可以include上面的titlebar.xml達到復用的效果,布局代碼如下

        <LinearLayout xmlns:android="
    
    
     android:orientation="vertical"   
     android:layout_width=”match_parent”  
     android:layout_height=”match_parent”  
     android:background="@color/app_bg"  
     android:gravity="center_horizontal">  
    
     <include  android:id="@+id/new_title"  
               layout="@layout/titlebar"/>  
    
     <TextView android:layout_width=”match_parent”  
               android:layout_height="wrap_content"  
               android:text="@string/hello"  
               android:padding="10dp" />  
    
     ...  
    
    

    </LinearLayout> </pre>
    通過取得元素的id,我們可以修改include標簽中元素的屬性,下面的例子為在actiivty中修改titlebar中的圖片:


        private View mTitleBar = null;
    private ImageView mTitleImageView = null;

    mTitleBar = findViewById(R.id.newtitle);
    mTitleImageView = (ImageView)mTitleBar.findViewById(R.id.title);
    mTitleImageView.setImageResource(R.drawable.logo); </pre>
    在使用include標簽時,我們可以覆寫插入布局root view的屬性(所有的android:layout
    *屬性)



        <include android:id=”@+id/news_title”

          android:layout_width=”match_parent”  
          android:layout_height=”match_parent”  
          layout=”@layout/title”/>  </pre> <p><br />
    

    </p>

    如果需要覆寫插入布局root view的屬性,則必須制定android:layout_width和android:layout_height這兩個屬性以使其它的覆寫屬性生效。

    注意:被引用的布局的屬性中,只有,外層的layout屬性可以被修改,內部屬性不能被修改。

    舉例:button2.xml,內容如下:

    <Button xmlns:android="
    

    android:id="@+id/button1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Button" /></pre>

    那么在別的地方引用這個布局:
            <include
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button23456"</span>
                layout="@layout/button2" />
    </div>

     那么,綠色的代碼設置的屬性是有效的,紅色的代碼設置的屬性是無效的。

    其實,這很容易理解,就是include是為了復用一段封裝好的布局,那么,布局內部的東西自然是不用改的,如果要改,還不如在被引用的文件中改好,我們在include中修改的只是被引用的布局的大小位置。

    3. 使用<merge/>標簽

    merge標簽用來消除我們在include一個布局到另一個布局時所產生的冗余view group。比如現在很多布局中會有兩個連續的Button,于是我們將這兩個連續的Button做成可復用布局(re-usable layout)。在使用include標簽時我們必須先將這兩個Button用一個view group比如LinearLayout組織在一起然后供其它布局使用,如果是include的地方也是LiearLayout就會造成有兩層連續的LiearLayout,除了降低UI性能沒有任何好處。這個時候我們就可以使用<merge/>標簽作為可復用布局的root view來避免這個問題。

        <merge xmlns: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> </pre>
    當我們用<include/>標簽復用上述代碼時,系統會忽略merge元素,直接將兩個連續的Button放在<include/>標簽所在處。

 本文由用戶 pxdb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!