[Go] Add fragments dynamically

Source: Internet
Author: User

This chapter is translated from "beginning-android-4-application-development", if there is a place where translation is inappropriate, please note.

Original book purchase address http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/

The real use of fragment is to add dynamically during the program's run.

1. New project.

2. Res/layout/main.xml

[Java]View Plaincopy
    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="Horizontal" >
    6. </LinearLayout>

3. Res/layout/fragment1.xml

[Java]View Plaincopy
  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:background="#00FF00"
  6. android:orientation="vertical" >
  7. <textview
  8. android:id="@+id/lblfragment1"
  9. Android:layout_width="Fill_parent"
  10. android:layout_height="Wrap_content"
  11. android:text="This is fragment #1"
  12. android:textcolor="#000000"
  13. android:textsize="25sp"/>
  14. </LinearLayout>

4. Res/layout/fragment2.xml

[Java]View Plaincopy
  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:background="#FFFE00"
  6. android:orientation="vertical" >
  7. <textview
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="This is fragment #2"
  11. android:textcolor="#000000"
  12. android:textsize="25sp"/>
  13. </LinearLayout>

5. Fragment1.java

[Java]View Plaincopy
  1. Public class Fragment1 extends Fragment {
  2. @Override
  3. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  4. Bundle savedinstancestate) {
  5. //---Inflate the layout for this fragment---
  6. return Inflater.inflate (R.layout.fragment1, container, false);
  7. }
  8. }

6. Fragment2.java

[Java]View Plaincopy
  1. Public class Fragment2 extends Fragment {
  2. @Override
  3. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  4. Bundle savedinstancestate) {
  5. //---Inflate the layout for this fragment---
  6. return Inflater.inflate (R.layout.fragment2, container, false);
  7. }
  8. }

7. Fragmentsactivity.java

[Java]View Plaincopy
  1. Public class Fragmentsactivity extends Activity {
  2. /** Called when the activity is first created. * /
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.main);
  7. Fragmentmanager Fragmentmanager = Getfragmentmanager ();
  8. Fragmenttransaction fragmenttransaction = Fragmentmanager
  9. . BeginTransaction ();
  10. //---Get the current display info---
  11. WindowManager wm = Getwindowmanager ();
  12. Display d = wm.getdefaultdisplay ();
  13. if (d.getwidth () > D.getheight ()) {
  14. //---landscape mode---
  15. Fragment1 fragment1 = new Fragment1 ();
  16. //Android. R.id.content refers to the content
  17. //View of the activity
  18. Fragmenttransaction.replace (Android. R.id.content, Fragment1);
  19. } Else {
  20. //---portrait mode---
  21. Fragment2 Fragment2 = new Fragment2 ();
  22. Fragmenttransaction.replace (Android. R.id.content, Fragment2);
  23. }
  24. //---Add to the back stack---
  25. Fragmenttransaction.addtobackstack (null);
  26. Fragmenttransaction.commit ();
  27. }
  28. }

8. Commissioning.

[Go] Add fragments dynamically

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.