1. 程式人生 > >如何獲取螢幕的狀態以及橫屏與豎屏自動切換

如何獲取螢幕的狀態以及橫屏與豎屏自動切換

demo1:獲取螢幕的狀態 ,橫屏與豎屏自動切換fragment

  1. publicclassMainActivityextendsActivity{
  2. @Override
  3. protectedvoid onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. //獲取螢幕的寬度和高度 適配不同的fragment
  7. DisplayMetrics outMetrics=newDisplayMetrics();
  8. getWindowManager
    ().getDefaultDisplay().getMetrics(outMetrics);
  9. int width=outMetrics.widthPixels;
  10. int height=outMetrics.heightPixels;
  11. RelativeLayout layout=(RelativeLayout)findViewById(R.id.layoutId);
  12. //要藉助此物件管理fragment物件
  13. FragmentManager fm= getFragmentManager();
  14. FragmentTransaction ft=fm.beginTransaction();
  15. //使用不同的fragment
  16. if(
    height>width){
  17. ft.replace(R.id.layoutId,newFragment01(),"tag");
  18. }else{
  19. ft.replace(R.id.layoutId,newFragment02(),"tag");
  20. }
  21. //可選:將fragment新增到回退棧
  22. //ft.addToBackStack(null);
  23. ft.commit();
  24. }
  25. @Override
  26. protectedvoid onResume(){
  27. super.onResume();
  28. //根據tag找到對應的fragment
  29. Fragment f=getFragmentManager().findFragmentByTag
    ("tag");
  30. Log.i("TAG","f="+f);
  31. }
  32. }

demo2:Tab+fragment 實現螢幕旋轉儲存資料

  1. publicclassMainActivityextendsActivity{
  2. ActionBar bar;
  3. @Override
  4. protectedvoid onCreate(Bundle savedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. bar=getActionBar();
  8. bar.setDisplayShowHomeEnabled(false);
  9. bar.setDisplayShowTitleEnabled(false);
  10. //設定為tab導航
  11. bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  12. //新增tab
  13. bar.addTab(bar.newTab().setText("tab01").setTabListener(
  14. newMyTabListener(this,Fragment01.class.getName(),"A")));
  15. bar.addTab(bar.newTab().setText("tab02").setTabListener(
  16. newMyTabListener(this,Fragment02.class.getName(),"B")));
  17. }
  18. @Override
  19. protectedvoid onRestoreInstanceState(Bundle savedInstanceState){
  20. super.onRestoreInstanceState(savedInstanceState);
  21. if(savedInstanceState!=null){
  22. int pos=savedInstanceState.getInt("posKey");
  23. bar.setSelectedNavigationItem(pos);
  24. }
  25. }
  26. privateclassMyTabListenerimplementsTabListener{
  27. privateContext context;
  28. privateString className;
  29. privateFragment frg;
  30. privateString tag;
  31. publicMyTabListener(Context context,String className,String tag){
  32. this.context=context;
  33. this.className=className;
  34. this.tag=tag;
  35. //檢測在當前activity是否已經有存在的fragment
  36. Fragment frg=getFragmentManager().findFragmentByTag(tag);
  37. if(frg!=null&&!frg.isDetached()){
  38. FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
  39. beginTransaction.detach(frg);
  40. beginTransaction.commit();
  41. }
  42. }
  43. @Override
  44. publicvoid onTabSelected(Tab tab,FragmentTransaction ft){
  45. if(frg==null){
  46. frg=Fragment.instantiate(context, className);
  47. ft.add(R.id.layout,frg,tag);
  48. }else{
  49. ft.attach(frg);
  50. }
  51. }
  52. //沒有選中的那個在旋轉需要解除關聯
  53. @Override
  54. publicvoid onTabUnselected(Tab tab,FragmentTransaction ft){
  55. if(frg!=null){
  56. ft.detach(frg);
  57. }
  58. }
  59. @Override
  60. publicvoid onTabReselected(Tab tab,FragmentTransaction ft){
  61. }
  62. }
  63. @Override
  64. protectedvoid onSaveInstanceState(Bundle outState){
  65. super.onSaveInstanceState(outState);
  66. int pos=bar.getSelectedTab().getPosition();
  67. outState.putInt("posKey", pos);
  68. }
  69. }