android - 安卓怎么固定屏幕两个方向旋转?
怪我咯
怪我咯 2017-04-17 17:20:37
0
2
666

我现在做的应用屏幕左侧旋转的时候 程序有问题,会崩溃,所以想只允许用户可以向左侧和上方两个方向上旋转屏幕,想问一下大家有知道怎么实现吗?网上查到的资料都是怎么去固定一个方向的,没有找到这方面资料,希望了解的朋友告知一声,谢谢!

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
PHPzhong

You need to know the following points

1.Settings

<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>

2. Force the screen rotation effect to be turned on

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

3.监听屏幕旋转

①调用当前的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>

3. Monitor screen rotation

①Call onSaveInstanceState(Bundle outState) of the current Activity to save some data in the application. You can override this method

②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, one thing that needs to be explained is how to use onSaveInstanceState(Bundle outState): This method will be called when the screen is rotated. The parameter Bundle here is to encapsulate what we want before the screen is rotated. of the saved data, then call onDestroy() to destroy the current Activity, and finally call onCreate(Bundle savedInstanceState) again. At this time, outState will be assigned to savedInstanceState. In this case, when we re-create the Activity in onCreate, we can use the previously saved This is very important because during the entire running process of an application, regardless of whether it is in horizontal or vertical screen, the data changes must be consistent. The user cannot be asked to start from scratch just because the screen changes. Start operating again

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 🎜

🎜Here’s how: 🎜 🎜First: Permission Statement: 🎜 🎜<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🎜
Ty80

Although you didn’t answer my question directly, I used what you said today. Thank you very much.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template