Android裡merge和include標籤的使用

來源:互聯網
上載者:User
Android裡merge和include標籤的使用參考android文檔:《Layout Tricks:Merging》

先得說下關於<merge />標籤的第一個比較簡單的用法。如果我們使用FrameLayout作為activity's content view的父元素(也就是在main.xml裡把它寫在最外層),那麼可以考慮用<merge />替換<FrameLayout />標籤。官方文檔給出的解釋是這樣做可以減少一級布局層次達到最佳化布局的效果。這是文檔裡關於這部分結論的原文,個人E文水平有限,直接貼出來好了:
Obviously, using <merge /> works in this case because the parent of an activity's content view is always a FrameLayout. You could not apply this trick if your layout was using a LinearLayout as its root tag for instance.

關於merge和include標籤的使用,直接用執行個體說明吧。
TestMergeInclude.java
public class TestMergeInclude extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:okCancelBar="http://schemas.android.com/apk/res/test.mergeinclude">

<ImageView  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

android:scaleType="center"
android:src="@drawable/wallpaper_rainbokeh" />

<test.mergeinclude.OkCancelBar
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="bottom"

android:paddingTop="8dip"
android:gravity="center_horizontal"

android:background="#AA000000"

okCancelBar:okLabel="Save"
okCancelBar:cancelLabel="Don't save" />
</merge>

OkCancelBar.java
public class OkCancelBar extends LinearLayout {

public OkCancelBar(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public OkCancelBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub

setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER);
setWeightSum(1.0f);

LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);

String text = array.getString(R.styleable.OkCancelBar_okLabel);
if(text == null) text = "Ok";
((Button)findViewById(R.id.okcancelbar_ok)).setText(text);

text = array.getString(R.styleable.OkCancelBar_cancelLabel);
if(text == null) text = "Cancel";
((Button)findViewById(R.id.okcancelbar_cancel)).setText(text);

array.recycle();
}
}

okcancelbar.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<include
layout="@layout/okcancelbar_button"
android:id="@+id/okcancelbar_ok" />

<include
layout="@layout/okcancelbar_button"
android:id="@+id/okcancelbar_cancel" />
</merge>

okcancelbar_button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

一點思考:
1.在OkCancelBar類的構造器中,我們看到一段稍微複雜點兒的代碼。其實它們並不難,只是通過ID拿到某個特定的Button引用,然後為其設定要顯示出來的文字。與直接使用android:text相比,這種設定要累贅得多。所以不妨把這個累贅看成是為利用<merge />的優點所付出的代價。這個優點就是組件重用,我們只在
okcancelbar_button.xml檔案裡聲明了一個Button,但在使用時卻擁有了兩個(當然可以更多)Button。

2.這裡可重用組件(這裡的okcancelbar_button)的定義很關鍵,因為它關聯著所有要使用它的功能點。比如:如果你確定在你的應用裡,所有使用到這個button的地方需要的顯示文字都一樣,那你就可以在上面button的定義中再加一個android:text屬性,這樣就省去了使用時再去逐個設定的麻煩。另外,本例的okcancelbar_button裡只定義了一個button,我想這裡應該可以擴充到一個布局單元,比如LinearLayout,FrameLayout等等之類的。本人還沒嘗試,值得一做。

3.關於使用<merge />標籤的一些限制:
(1)它只能作為XML布局聲明的root元素來使用;
(2)使用它來inflate一個布局時,必須指定一個ViewGroup執行個體作為其父元素並且設定attachToRoot屬性為true(參考 inflate(int, android.view.ViewGroup, boolean) 方法的說明 )。轉自:http://hi.baidu.com/lck0502/item/0b090cae8ac21b3e030a4d62

相關文章

聯繫我們

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