您好,登录后才能下订单哦!
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。