The examples in this article describe how Android uses include to invoke internal components. Share to everyone for your reference. Specifically as follows:
Example one:
Sublayout.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Wrap_content " android:background= "#505050" > <textview android:layout_width= "fill_parent android:layout_height=" Wrap_ Content "android:text=" Sublayout "/> <button android:id=" @+id/mybutton "android:layout_width=" fill_parent "Android:layout_height=" wrap_content "android:text=" A button "/> </LinearLayout> mail.xml <?xml
Version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <T Extview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/hello" > <include android:id= "@+id/maIn1 "layout=" @layout/sublayout "/> <include android:id=" @+id/main2 "layout=" @layout/sublayout "/> <Button
Android:id= "@+id/startanotheractivity" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"
android:text= "Start Another activity"/> </LinearLayout>
How to invoke the component included in the component.
Package com.
Androidincludelayout;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
The public class Androidincludelayout extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Override
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
View SUBLAYOUT1 = (view) Findviewbyid (r.id.main1);
View SubLayout2 = (view) Findviewbyid (r.id.main2);
Button mybutton_main1 = (button) Sublayout1.findviewbyid (R.id.mybutton);
Button mybutton_main2 = (button) Sublayout2.findviewbyid (R.id.mybutton);
Button startanotheractivity = (button) Findviewbyid (r.id.startanotheractivity);
Startanotheractivity.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) { TODO auto-generated Method Stub Intent Intent = new IntenT ();
Intent.setclass (Androidincludelayout.this, Anotheractivity.class);
StartActivity (Intent);
}}); Mybutton_main1.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) {//TOD
O auto-generated Method Stub toast.maketext (androidincludelayout.this, "button 1 pressed", Toast.length_long). Show ();
}}); Mybutton_main2.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) {//TOD
O auto-generated Method Stub toast.maketext (androidincludelayout.this, "button 2 pressed", Toast.length_long). Show ();
}}); }
}
But if the included XML is
Sublayout.xml
<?xml version= "1.0" encoding= "Utf-8"?> <merge xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<textview
android:layout_width=" fill_parent "
android:layout_height=" Wrap_content "
android:text= "sublayout"
/>
<button
android:id=
"@+id/mybutton" android:layout_width = "Fill_parent"
android:layout_height= "wrap_content"
android:text= "A button"
/>
</ Merge>
The above method will not be implemented and a null pointer will be reported.
Because after using the merge, the import is equivalent to the current view of the component, so direct findviewbyid on it.
In this case ... You can solve the problem of include multiple times with the same layout
I hope this article will help you with your Android program.