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
- <?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="Horizontal" >
- </LinearLayout>
3. Res/layout/fragment1.xml
[Java]View Plaincopy
- <?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:background="#00FF00"
- android:orientation="vertical" >
- <textview
- android:id="@+id/lblfragment1"
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:text="This is fragment #1"
- android:textcolor="#000000"
- android:textsize="25sp"/>
- </LinearLayout>
4. Res/layout/fragment2.xml
[Java]View Plaincopy
- <?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:background="#FFFE00"
- android:orientation="vertical" >
- <textview
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:text="This is fragment #2"
- android:textcolor="#000000"
- android:textsize="25sp"/>
- </LinearLayout>
5. Fragment1.java
[Java]View Plaincopy
- Public class Fragment1 extends Fragment {
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- //---Inflate the layout for this fragment---
- return Inflater.inflate (R.layout.fragment1, container, false);
- }
- }
6. Fragment2.java
[Java]View Plaincopy
- Public class Fragment2 extends Fragment {
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- //---Inflate the layout for this fragment---
- return Inflater.inflate (R.layout.fragment2, container, false);
- }
- }
7. Fragmentsactivity.java
[Java]View Plaincopy
- Public class Fragmentsactivity extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Fragmentmanager Fragmentmanager = Getfragmentmanager ();
- Fragmenttransaction fragmenttransaction = Fragmentmanager
- . BeginTransaction ();
- //---Get the current display info---
- WindowManager wm = Getwindowmanager ();
- Display d = wm.getdefaultdisplay ();
- if (d.getwidth () > D.getheight ()) {
- //---landscape mode---
- Fragment1 fragment1 = new Fragment1 ();
- //Android. R.id.content refers to the content
- //View of the activity
- Fragmenttransaction.replace (Android. R.id.content, Fragment1);
- } Else {
- //---portrait mode---
- Fragment2 Fragment2 = new Fragment2 ();
- Fragmenttransaction.replace (Android. R.id.content, Fragment2);
- }
- //---Add to the back stack---
- Fragmenttransaction.addtobackstack (null);
- Fragmenttransaction.commit ();
- }
- }
8. Commissioning.
[Go] Add fragments dynamically