Include for Android layout xml

Source: Internet
Author: User

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:

  1. <?xml version= "1.0" encoding= "Utf-8"?>

  2. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

  3. Android:layout_width= "Fill_parent"

  4. android:layout_height= "Wrap_content"

  5. android:orientation= "Vertical" >

  6. <button

  7. Android:id= "@+id/btn"

  8. Android:layout_width= "Wrap_content"

  9. android:layout_height= "Wrap_content"

  10. android:text= "button" >

  11. </Button>

  12. </LinearLayout>

Copy Code

Main.xml

  1. <?xml version= "1.0" encoding= "Utf-8"?>

  2. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

  3. Android:layout_width= "Fill_parent"

  4. android:layout_height= "Fill_parent"

  5. android:orientation= "Vertical"

  6. >

  7. <include android:id= "@+id/in1" layout= "@layout/btn"/>

  8. <include android:id= "@+id/in2" layout= "@layout/btn"/>

  9. <textview android:id= "@+id/tv"

  10. Android:layout_width= "Fill_parent"

  11. android:layout_height= "Wrap_content"

  12. android:text= "@string/hello"/>

  13. </LinearLayout>

Copy Code

Testactivity:

  1. Package com.hilary;


  2. Import android.app.Activity;

  3. Import Android.graphics.Color;

  4. Import Android.os.Bundle;

  5. Import Android.view.View;

  6. Import Android.view.View.OnClickListener;

  7. Import Android.widget.Button;

  8. Import Android.widget.LinearLayout;

  9. Import Android.widget.TextView;


  10. Import COM.HIALRY.R;

  11. /**

  12. *

  13. * @author: Hilary

  14. * @Date: 2011-12-8

  15. * @description:

  16. *

  17. **/

  18. public class Testactivity extends Activity {

  19. Private TextView TV = null;

  20. Private LinearLayout LL = null;

  21. Private LinearLayout ll2 = null;


  22. /** called when the activity is first created. */

  23. @Override

  24. public void OnCreate (Bundle savedinstancestate) {

  25. Super.oncreate (savedinstancestate);

  26. Setcontentview (R.layout.main);

  27. TV = (TextView) Findviewbyid (r.id.tv);

  28. 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.

  29. This step is not required if the same XML file side is included

  30. ll = (linearlayout) Findviewbyid (r.id.in1);

  31. Ll2 = (linearlayout) Findviewbyid (r.id.in2);


  32. Ll.setbackgroundcolor (color.red);


  33. Button btn = (button) Ll.findviewbyid (R.ID.BTN);

  34. Btn.setonclicklistener (New Onclicklistener () {


  35. @Override

  36. public void OnClick (View v) {

  37. Tv.settext ("My name is Hilary");

  38. }

  39. });


  40. Button btn2 = (button) Ll2.findviewbyid (R.ID.BTN);

  41. Btn2.setonclicklistener (New Onclicklistener () {


  42. @Override

  43. public void OnClick (View v) {

  44. Tv.settext ("You select Second button!");


  45. }

  46. });

  47. }

  48. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.