Android中怎么實現(xiàn)橫豎屏幕切換生命周期,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)梅列免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
一、簡介
二、代碼
/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;//創(chuàng)建一個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被創(chuàng)建時執(zhí)行 */ Log.d(tag, "onCreate"); } /* * activity可見時執(zhí)行 */ @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.d(tag, "onStart"); } /* * activity交互時執(zhí)行 */ @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.d(tag, "onResume"); } /* * activity重新可見時執(zhí)行 */ @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.d(tag, "onRestart"); } /* * activity暫停時執(zhí)行 */ @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.d(tag, "onPause"); } /* * activity停止時執(zhí)行 */ @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.d(tag, "onStop"); } /* * activity銷毀時執(zhí)行 */ @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d(tag, "onDestroy"); } /* * activity在配置改變時執(zhí)行 * 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變 */ @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://斷開狀態(tài) finish(); default: break; } } }
三、一直橫屏或者一直豎屏
很多手機(jī)游戲里面一進(jìn)去就是橫屏,而且不能切換為豎屏,那么怎么樣達(dá)到這樣的效果呢?
/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"豎屏
四、如何獲取手機(jī)是橫屏還是豎屏
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;//創(chuàng)建一個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被創(chuàng)建時執(zhí)行 */ Log.d(tag, "onCreate"); } /* * activity可見時執(zhí)行 */ @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.d(tag, "onStart"); } /* * activity交互時執(zhí)行 */ @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.d(tag, "onResume"); } /* * activity重新可見時執(zhí)行 */ @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.d(tag, "onRestart"); } /* * activity暫停時執(zhí)行 */ @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.d(tag, "onPause"); } /* * activity停止時執(zhí)行 */ @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.d(tag, "onStop"); } /* * activity銷毀時執(zhí)行 */ @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d(tag, "onDestroy"); } /* * activity在配置改變時執(zhí)行 * 比如橫豎屏幕的切換,鍵盤有無的切換,屏幕大小的改變 */ @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://斷開狀態(tài) 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>
關(guān)于Android中怎么實現(xiàn)橫豎屏幕切換生命周期問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
當(dāng)前標(biāo)題:Android中怎么實現(xiàn)橫豎屏幕切換生命周期
分享URL:http://www.rwnh.cn/article44/jeeche.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、全網(wǎng)營銷推廣、軟件開發(fā)、商城網(wǎng)站、搜索引擎優(yōu)化、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)