我现在做的应用屏幕左侧旋转的时候 程序有问题,会崩溃,所以想只允许用户可以向左侧和上方两个方向上旋转屏幕,想问一下大家有知道怎么实现吗?网上查到的资料都是怎么去固定一个方向的,没有找到这方面资料,希望了解的朋友告知一声,谢谢!
PHPz2017-04-17 17:22:37
<activity>The android:screenOrientation attribute of the node can complete this task. The sample code is as follows:
<activity android:name=".EX01"
android:label="@string/app_name"
android:screenOrientation="portrait">
//值为portrait时强制为竖屏, 值为landscape时强制为横屏
</activity>
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
①调用当前的Activity的onSaveInstanceState(Bundle outState)去保存应用中的一些数据,你可以重写这个方法
②然后调用onDestroy()销毁当前的Activity
③重新调用onCreate()或onRestoreInstanceState()方法去重新创建一个Activity(也就是说,程序会重新将onCreate中的代码在执行一遍)
除此之外,还要说明的一点是onSaveInstanceState(Bundle outState)到底怎么用:当屏幕发生旋转的时候会调用这个方法,这里的参数Bundle,就是让我们来封装在屏幕 未旋转前时想要保存的数据的,之后调用onDestroy()销毁当前的Activity,最后重新调用onCreate(Bundle savedInstanceState),这时outState就会赋给savedInstanceState,这样的话,我们在onCreate中重新创建Activity的时候,可以用之前保存的数据来初始化重新创建的Activity,这很重要,因为一个应用程序在整个运行过程中,不论在横屏和竖屏,数据的变化都要有一致性,不能因为屏幕发生改变,就让用户从头开始重新操作一遍
除此之外,由于屏幕旋转时,一定会触发onSaveInstanceState(Bundle outState)方法,那么我们完全可以将这个方法作为屏幕旋转的监听器
下面就举一个例子:
*注意判断横屏和竖屏并不是简单的事,因为Android中规定,只有是完全竖和横才能够返回ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE、ActivityInfo.SCREEN_ORIENTATION_PORTRAIT 而这在实际中几乎无法存在这样的情况
这里是方法:
<uses-permission Android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
②Then call onDestroy() to destroy the current Activity
③Recall the onCreate() or onRestoreInstanceState() method to recreate an Activity (that is, the program will re-execute the code in onCreate)
In addition, since the onSaveInstanceState(Bundle outState) method will definitely be triggered when the screen rotates, then we can completely use this method as a monitor for screen rotation🎜 🎜Here’s an example: 🎜 🎜*Note that it is not a simple matter to judge the horizontal screen or the vertical screen, because Android stipulates that only when the screen is completely vertical or horizontal can ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE and ActivityInfo.SCREEN_ORIENTATION_PORTRAIT be returned, which is almost impossible in practice. Such a situation exists 🎜
<uses-permission Android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
🎜
🎜Second: Declare the event type to be captured by the activity, 🎜
<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="orientation|keyboard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
🎜The Android:configChanges attribute must be declared here. This attribute specifies the event types we can capture in the program. Multiple event types are separated by |. 🎜
🎜If there is no orientation here, then we cannot capture the screen change event in the program. 🎜
🎜Third: Rewrite the onConfigurationChanged method in Activity🎜
/**
* 屏幕旋转时调用此方法
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//newConfig.orientation获得当前屏幕状态是横向或者竖向
//Configuration.ORIENTATION_PORTRAIT 表示竖向
//Configuration.ORIENTATION_LANDSCAPE 表示横屏
if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(MainActivity.this, "现在是竖屏", Toast.LENGTH_SHORT).show();
}
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
Toast.makeText(MainActivity.this, "现在是横屏", Toast.LENGTH_SHORT).show();
}
}
🎜If you really don’t know how, you can contact me on the site. I can help you take a look and we can make progress together🎜PHP中文网2017-04-17 17:22:37
Although you didn’t answer my question directly, I used what you said today. Thank you very much.