码迷,mamicode.com
首页 > 其他好文 > 详细

[转]动态添加Fragments

时间:2015-11-23 21:54:00      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/

 

fragment的真正用处是在程序运行过程中动态地添加。

1. 新建工程。

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.   
  7. </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.   
  8.     <TextView  
  9.         android:id="@+id/lblFragment1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="This is fragment #1"  
  13.         android:textColor="#000000"  
  14.         android:textSize="25sp" />  
  15.   
  16. </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.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="This is fragment #2"  
  12.         android:textColor="#000000"  
  13.         android:textSize="25sp" />  
  14.   
  15. </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.   
  8.         FragmentManager fragmentManager = getFragmentManager();  
  9.         FragmentTransaction fragmentTransaction = fragmentManager  
  10.                 .beginTransaction();  
  11.   
  12.         // ---get the current display info---  
  13.         WindowManager wm = getWindowManager();  
  14.         Display d = wm.getDefaultDisplay();  
  15.         if (d.getWidth() > d.getHeight()) {  
  16.             // ---landscape mode---  
  17.             Fragment1 fragment1 = new Fragment1();  
  18.             // android.R.id.content refers to the content  
  19.             // view of the activity  
  20.             fragmentTransaction.replace(android.R.id.content, fragment1);  
  21.         } else {  
  22.             // ---portrait mode---  
  23.             Fragment2 fragment2 = new Fragment2();  
  24.             fragmentTransaction.replace(android.R.id.content, fragment2);  
  25.         }  
  26.         // ---add to the back stack---  
  27.         fragmentTransaction.addToBackStack(null);  
  28.         fragmentTransaction.commit();  
  29.   
  30.     }  
  31.   
  32. }  

8. 调试。

技术分享

 

技术分享

[转]动态添加Fragments

标签:

原文地址:http://www.cnblogs.com/xunbu7/p/4989598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!