鏈珷鑺傜炕璇戣嚜銆夿eginning-Android-4-Application-Development銆嬶紝濡傛湁缈昏瘧涓嶅綋鐨勫湴鏂癸紝鏁鎸囧嚭銆?/em>

鍘熶功璐拱鍦板潃http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


fragment鐨勭湡姝g敤澶勬槸鍦ㄧ▼搴忚繍琛岃繃绋嬩腑鍔ㄦ€佸湴娣诲姞銆?br>

1. 鏂板缓宸ョ▼銆?/p>

2. res/layout/main.xml

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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 plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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="Thisisfragment#1"
  12. android:textColor="#000000"
  13. android:textSize="25sp"/>
  14. </LinearLayout>
4. res/layout/fragment2.xml [java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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="Thisisfragment#2"
  11. android:textColor="#000000"
  12. android:textSize="25sp"/>
  13. </LinearLayout>
5. Fragment1.java [java] view plain copy
  1. publicclassFragment1extendsFragment{
  2. @Override
  3. publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
  4. BundlesavedInstanceState){
  5. //---Inflatethelayoutforthisfragment---
  6. returninflater.inflate(R.layout.fragment1,container,false);
  7. }
  8. }
6. Fragment2.java [java] view plain copy
  1. publicclassFragment2extendsFragment{
  2. @Override
  3. publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
  4. BundlesavedInstanceState){
  5. //---Inflatethelayoutforthisfragment---
  6. returninflater.inflate(R.layout.fragment2,container,false);
  7. }
  8. }

7. FragmentsActivity.java

[java] view plain copy
  1. publicclassFragmentsActivityextendsActivity{
  2. /**Calledwhentheactivityisfirstcreated.*/
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. FragmentManagerfragmentManager=getFragmentManager();
  8. FragmentTransactionfragmentTransaction=fragmentManager
  9. .beginTransaction();
  10. //---getthecurrentdisplayinfo---
  11. WindowManagerwm=getWindowManager();
  12. Displayd=wm.getDefaultDisplay();
  13. if(d.getWidth()>d.getHeight()){
  14. //---landscapemode---
  15. Fragment1fragment1=newFragment1();
  16. //android.R.id.contentreferstothecontent
  17. //viewoftheactivity
  18. fragmentTransaction.replace(android.R.id.content,fragment1);
  19. }else{
  20. //---portraitmode---
  21. Fragment2fragment2=newFragment2();
  22. fragmentTransaction.replace(android.R.id.content,fragment2);
  23. }
  24. //---addtothebackstack---
  25. fragmentTransaction.addToBackStack(null);
  26. fragmentTransaction.commit();
  27. }
  28. }

8. 璋冭瘯銆?/p>



更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Unable to execute dex: Multiple dex fi
  2. Android(安卓)ScaleType 详解,使用
  3. 后浪,谈谈你对jvm性能调优的理解
  4. Android中如何获取Bitmap(总结)
  5. Android实验一(在Android(安卓)Studio中创
  6. 下拉刷新相关
  7. Android(安卓)修改Camera拍照的默认保存
  8. 阿里云面试(android)
  9. Android程序中任意位置获取Context
  10. Android(安卓)获取未读未接来电和未读短