In a project we may need to use the same layout design, if it is written in an XML file, the code is very redundant, and very poor readability, so we can put the same layout of the code to write a separate module, and then use the time can be <include/> Label to reuse the layout code.
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>
Copy Code
Main.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= "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"/>
-
</LinearLayout>
Copy Code
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);
-
If a layout file contains the same XML file, the control IDs in the two XML are the same, and when these controls need to be manipulated, they need to be distinguished by defining the two view.
-
This step is not required if the same XML file side is included
-
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 () {
-
-
@Override
-
public void OnClick (View v) {
-
Tv.settext ("You select Second button!");
-
-
}
-
});
-
}
-
}
Copy Code
without the include tag, all layout code is written in an XML file, and the interface appears redundant and poorly readable. And when the interface is loaded in order, the previous layout cannot call the layout id behind it. With include, you can refer to the layout id attribute in the following include in an include-->
Include for Android layout xml