需要包含的xml檔案,我這裡就放了一個button按鈕:
btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button">
</button>
</linearlayout>
main.xml
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include android:id="@+id/in1" layout="@layout/btn"/>
<include android:id="@+id/in2" layout="@layout/btn"/>
<textview android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
testactivity:
package com.hilary;
import android.app.activity;
import android.graphics.color;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.linearlayout;
import android.widget.textview;
import com.hialry.r;
/**
*
*@author:hilary
*@date:2011-12-8
*@description:
**/
public class testactivity extends activity {
private textview tv = null;
private linearlayout ll = null;
private linearlayout ll2 = null;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
tv = (textview) findviewbyid(r.id.tv);
//如果一個布局檔案中包含同一個xml檔案,這兩個xml中的控件id是一樣的,當需要操作這些控件時,需要通過定義這兩個view來加以區分,
//如果就包含同一個xml檔案側不需要此步操作
ll = (linearlayout) findviewbyid(r.id.in1);
ll2 = (linearlayout) findviewbyid(r.id.in2);
ll.setbackgroundcolor(color.red);
button btn = (button) ll.findviewbyid(r.id.btn);
btn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
tv.settext("my name is hilary");
}
});
button btn2 = (button) ll2.findviewbyid(r.id.btn);
btn2.setonclicklistener(new onclicklistener() {
tv.settext(" you select second button!");
}
}
這隻是在xml檔案中引入另一種布局的一種方法,我們還可以在代碼中直接引入,而不需要在xml中定義要引入的檔案,在這裡就不多說了
from:http://hilary3113.iteye.com/blog/1297416
歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/p/4819479.html