您好,登錄后才能下訂單哦!
Android中怎么實現橫豎屏幕切換生命周期,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、簡介
二、代碼
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fry.activityLifeCycle_3Screen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="com.fry.activityLifeCycle_3Screen.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> </application> </manifest> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fry.activityLifeCycle_3Screen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="com.fry.activityLifeCycle_3Screen.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> </application> </manifest>
核心代碼:android:configChanges="keyboardHidden|orientation|screenSize"
com.fry.activityLifeCycle_3Screen.MainActivity
package com.fry.activityLifeCycle_3Screen; import com.fry.activityLifeCycle_3Screen.R; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ private Button btn_pause;//創建一個button對象 private Button btn_stop; private Button btn_offLine; private String tag=MainActivity.class.getSimpleName(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//父類操作 setContentView(R.layout.activity_main);//引入名為activity_main的界面 btn_pause=(Button) findViewById(R.id.btn_pause);//找id為btn_pause的button btn_stop=(Button) findViewById(R.id.btn_stop);//找id為btn_stop的button btn_offLine=(Button) findViewById(R.id.btn_offLine); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_offLine.setOnClickListener(this); /* * activity被創建時執行 */ Log.d(tag, "onCreate"); } /* * activity可見時執行 */ @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.d(tag, "onStart"); } /* * activity交互時執行 */ @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.d(tag, "onResume"); } /* * activity重新可見時執行 */ @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.d(tag, "onRestart"); } /* * activity暫停時執行 */ @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.d(tag, "onPause"); } /* * activity停止時執行 */ @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.d(tag, "onStop"); } /* * activity銷毀時執行 */ @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d(tag, "onDestroy"); } /* * activity在配置改變時執行 * 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變 */ @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); Log.d(tag, "onConfigurationChanged"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_pause: Intent intent=new Intent(); intent.setClass(this, Activity02.class); startActivity(intent); break; case R.id.btn_stop: Intent intent2=new Intent(); intent2.setClass(this, Activity01.class); startActivity(intent2); break; case R.id.btn_offLine://斷開狀態 finish(); default: break; } } }
三、一直橫屏或者一直豎屏
很多手機游戲里面一進去就是橫屏,而且不能切換為豎屏,那么怎么樣達到這樣的效果呢?
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fry.activityLifeCycle_3Screen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:screenOrientation="portrait" android:name="com.fry.activityLifeCycle_3Screen.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> </application> </manifest>
android:screenOrientation="landscape"橫屏
android:screenOrientation="portrait"豎屏
四、如何獲取手機是橫屏還是豎屏
com.fry.activityLifeCycle_3Screen.MainActivity
package com.fry.activityLifeCycle_3Screen; import com.fry.activityLifeCycle_3Screen.R; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ private Button btn_pause;//創建一個button對象 private Button btn_stop; private Button btn_offLine; private String tag=MainActivity.class.getSimpleName(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//父類操作 setContentView(R.layout.activity_main);//引入名為activity_main的界面 btn_pause=(Button) findViewById(R.id.btn_pause);//找id為btn_pause的button btn_stop=(Button) findViewById(R.id.btn_stop);//找id為btn_stop的button btn_offLine=(Button) findViewById(R.id.btn_offLine); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_offLine.setOnClickListener(this); /* * activity被創建時執行 */ Log.d(tag, "onCreate"); } /* * activity可見時執行 */ @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.d(tag, "onStart"); } /* * activity交互時執行 */ @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.d(tag, "onResume"); } /* * activity重新可見時執行 */ @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.d(tag, "onRestart"); } /* * activity暫停時執行 */ @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.d(tag, "onPause"); } /* * activity停止時執行 */ @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.d(tag, "onStop"); } /* * activity銷毀時執行 */ @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d(tag, "onDestroy"); } /* * activity在配置改變時執行 * 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變 */ @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); Log.d(tag, "onConfigurationChanged"); int width=getWindowManager().getDefaultDisplay().getWidth(); int height=getWindowManager().getDefaultDisplay().getHeight(); if(width>height) Log.d(tag, "landscape"); else Log.d(tag, "portrait"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_pause: Intent intent=new Intent(); intent.setClass(this, Activity02.class); startActivity(intent); break; case R.id.btn_stop: Intent intent2=new Intent(); intent2.setClass(this, Activity01.class); startActivity(intent2); break; case R.id.btn_offLine://斷開狀態 finish(); default: break; } } }
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fry.activityLifeCycle_3Screen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="com.fry.activityLifeCycle_3Screen.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" ></activity> <activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" ></activity> </application> </manifest>
關于Android中怎么實現橫豎屏幕切換生命周期問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。