橫豎屏幕切換生命周期 一、簡介 二、代碼 /activityLifeCycle_3Screen/AndroidManifest.xml 核心代碼:android:configChanges="keyboardHidden|orientation|screenSize" com.fry.activit ...
橫豎屏幕切換生命周期
一、簡介
二、代碼
/activityLifeCycle_3Screen/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.fry.activityLifeCycle_3Screen" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity 16 android:configChanges="keyboardHidden|orientation|screenSize" 17 android:name="com.fry.activityLifeCycle_3Screen.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> 26 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> 27 </application> 28 29 </manifest> 30 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 31 package="com.fry.activityLifeCycle_3Screen" 32 android:versionCode="1" 33 android:versionName="1.0" > 34 35 <uses-sdk 36 android:minSdkVersion="8" 37 android:targetSdkVersion="19" /> 38 39 <application 40 android:allowBackup="true" 41 android:icon="@drawable/ic_launcher" 42 android:label="@string/app_name" 43 android:theme="@style/AppTheme" > 44 <activity 45 android:configChanges="keyboardHidden|orientation|screenSize" 46 android:name="com.fry.activityLifeCycle_3Screen.MainActivity" 47 android:label="@string/app_name" > 48 <intent-filter> 49 <action android:name="android.intent.action.MAIN" /> 50 51 <category android:name="android.intent.category.LAUNCHER" /> 52 </intent-filter> 53 </activity> 54 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> 55 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> 56 </application> 57 58 </manifest>
核心代碼:android:configChanges="keyboardHidden|orientation|screenSize"
com.fry.activityLifeCycle_3Screen.MainActivity
1 package com.fry.activityLifeCycle_3Screen; 2 3 4 5 6 7 import com.fry.activityLifeCycle_3Screen.R; 8 9 import android.app.Activity; 10 import android.content.Intent; 11 import android.content.res.Configuration; 12 import android.os.Bundle; 13 import android.util.Log; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 18 19 20 public class MainActivity extends Activity implements OnClickListener{ 21 private Button btn_pause;//創建一個button對象 22 private Button btn_stop; 23 private Button btn_offLine; 24 private String tag=MainActivity.class.getSimpleName(); 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState);//父類操作 27 setContentView(R.layout.activity_main);//引入名為activity_main的界面 28 btn_pause=(Button) findViewById(R.id.btn_pause);//找id為btn_pause的button 29 btn_stop=(Button) findViewById(R.id.btn_stop);//找id為btn_stop的button 30 btn_offLine=(Button) findViewById(R.id.btn_offLine); 31 btn_pause.setOnClickListener(this); 32 btn_stop.setOnClickListener(this); 33 btn_offLine.setOnClickListener(this); 34 /* 35 * activity被創建時執行 36 */ 37 Log.d(tag, "onCreate"); 38 39 40 } 41 /* 42 * activity可見時執行 43 */ 44 @Override 45 protected void onStart() { 46 // TODO Auto-generated method stub 47 super.onStart(); 48 Log.d(tag, "onStart"); 49 } 50 51 /* 52 * activity交互時執行 53 */ 54 @Override 55 protected void onResume() { 56 // TODO Auto-generated method stub 57 super.onResume(); 58 Log.d(tag, "onResume"); 59 } 60 /* 61 * activity重新可見時執行 62 */ 63 @Override 64 protected void onRestart() { 65 // TODO Auto-generated method stub 66 super.onRestart(); 67 Log.d(tag, "onRestart"); 68 } 69 /* 70 * activity暫停時執行 71 */ 72 @Override 73 protected void onPause() { 74 // TODO Auto-generated method stub 75 super.onPause(); 76 Log.d(tag, "onPause"); 77 } 78 /* 79 * activity停止時執行 80 */ 81 @Override 82 protected void onStop() { 83 // TODO Auto-generated method stub 84 super.onStop(); 85 Log.d(tag, "onStop"); 86 } 87 /* 88 * activity銷毀時執行 89 */ 90 @Override 91 protected void onDestroy() { 92 // TODO Auto-generated method stub 93 super.onDestroy(); 94 Log.d(tag, "onDestroy"); 95 } 96 /* 97 * activity在配置改變時執行 98 * 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變 99 */ 100 @Override 101 public void onConfigurationChanged(Configuration newConfig) { 102 // TODO Auto-generated method stub 103 super.onConfigurationChanged(newConfig); 104 Log.d(tag, "onConfigurationChanged"); 105 } 106 @Override 107 public void onClick(View v) { 108 // TODO Auto-generated method stub 109 switch (v.getId()) { 110 case R.id.btn_pause: 111 Intent intent=new Intent(); 112 intent.setClass(this, Activity02.class); 113 startActivity(intent); 114 break; 115 case R.id.btn_stop: 116 Intent intent2=new Intent(); 117 intent2.setClass(this, Activity01.class); 118 startActivity(intent2); 119 break; 120 case R.id.btn_offLine://斷開狀態 121 finish(); 122 default: 123 break; 124 } 125 } 126 }
三、一直橫屏或者一直豎屏
很多手機游戲裡面一進去就是橫屏,而且不能切換為豎屏,那麼怎麼樣達到這樣的效果呢?
/activityLifeCycle_3Screen/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.fry.activityLifeCycle_3Screen" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity 16 android:screenOrientation="portrait" 17 android:name="com.fry.activityLifeCycle_3Screen.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> 26 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> 27 </application> 28 29 </manifest>
android:screenOrientation="landscape"橫屏
android:screenOrientation="portrait"豎屏
四、如何獲取手機是橫屏還是豎屏
com.fry.activityLifeCycle_3Screen.MainActivity
1 package com.fry.activityLifeCycle_3Screen; 2 3 4 5 6 7 import com.fry.activityLifeCycle_3Screen.R; 8 9 import android.app.Activity; 10 import android.content.Intent; 11 import android.content.res.Configuration; 12 import android.os.Bundle; 13 import android.util.Log; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 18 19 20 public class MainActivity extends Activity implements OnClickListener{ 21 private Button btn_pause;//創建一個button對象 22 private Button btn_stop; 23 private Button btn_offLine; 24 private String tag=MainActivity.class.getSimpleName(); 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState);//父類操作 27 setContentView(R.layout.activity_main);//引入名為activity_main的界面 28 btn_pause=(Button) findViewById(R.id.btn_pause);//找id為btn_pause的button 29 btn_stop=(Button) findViewById(R.id.btn_stop);//找id為btn_stop的button 30 btn_offLine=(Button) findViewById(R.id.btn_offLine); 31 btn_pause.setOnClickListener(this); 32 btn_stop.setOnClickListener(this); 33 btn_offLine.setOnClickListener(this); 34 /* 35 * activity被創建時執行 36 */ 37 Log.d(tag, "onCreate"); 38 39 40 } 41 /* 42 * activity可見時執行 43 */ 44 @Override 45 protected void onStart() { 46 // TODO Auto-generated method stub 47 super.onStart(); 48 Log.d(tag, "onStart"); 49 } 50 51 /* 52 * activity交互時執行 53 */ 54 @Override 55 protected void onResume() { 56 // TODO Auto-generated method stub 57 super.onResume(); 58 Log.d(tag, "onResume"); 59 } 60 /* 61 * activity重新可見時執行 62 */ 63 @Override 64 protected void onRestart() { 65 // TODO Auto-generated method stub 66 super.onRestart(); 67 Log.d(tag, "onRestart"); 68 } 69 /* 70 * activity暫停時執行 71 */ 72 @Override 73 protected void onPause() { 74 // TODO Auto-generated method stub 75 super.onPause(); 76 Log.d(tag, "onPause"); 77 } 78 /* 79 * activity停止時執行 80 */ 81 @Override 82 protected void onStop() { 83 // TODO Auto-generated method stub 84 super.onStop(); 85 Log.d(tag, "onStop"); 86 } 87 /* 88 * activity銷毀時執行 89 */ 90 @Override 91 protected void onDestroy() { 92 // TODO Auto-generated method stub 93 super.onDestroy(); 94 Log.d(tag, "onDestroy"); 95 } 96 /* 97 * activity在配置改變時執行 98 * 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變 99 */ 100 @Override 101 public void onConfigurationChanged(Configuration newConfig) { 102 // TODO Auto-generated method stub 103 super.onConfigurationChanged(newConfig); 104 Log.d(tag, "onConfigurationChanged"); 105 int width=getWindowManager().getDefaultDisplay().getWidth(); 106 int height=getWindowManager().getDefaultDisplay().getHeight(); 107 if(width>height) Log.d(tag, "landscape"); 108 else Log.d(tag, "portrait"); 109 } 110 @Override 111 public void onClick(View v) { 112 // TODO Auto-generated method stub 113 switch (v.getId()) { 114 case R.id.btn_pause: 115 Intent intent=new Intent(); 116 intent.setClass(this, Activity02.class); 117 startActivity(intent); 118 break; 119 case R.id.btn_stop: 120 Intent intent2=new Intent(); 121 intent2.setClass(this, Activity01.class); 122 startActivity(intent2); 123 break; 124 case R.id.btn_offLine://斷開狀態 125 finish(); 126 default: 127 break; 128 } 129 } 130 }
/activityLifeCycle_3Screen/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.fry.activityLifeCycle_3Screen" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity 16 android:configChanges="keyboardHidden|orientation|screenSize" 17 android:name="com.fry.activityLifeCycle_3Screen.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> 26 <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> 27 </application> 28 29 </manifest>