简体   繁体   English

如何锁定Android屏幕方向

[英]How to lock android screen orientation

I'm making an Android video player.It has a function like that user can watch video in any orientation.I just use code as follow: 我正在制作一个Android视频播放器,它具有类似于用户可以在任何方向观看视频的功能,我只使用以下代码:

Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);

It works, but when I add a function that user can lock the orientation, I just did it: 它可以工作,但是当我添加一个用户可以锁定方向的功能时,我就做到了:

Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);

So I met some trouble. 所以我遇到了麻烦。 when I'm in landscape orientation and try to lock the orientation, the screen just turns to portrait. 当我处于横向方向并尝试锁定方向时,屏幕将变为纵向。 Can anyone solve it or tell me another way to do with it? 谁能解决这个问题或告诉我另一种处理方法?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

OR 要么

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   

More info here: Developing Orientation-Aware Android Applications 此处了解更多信息: 开发可识别方向的Android应用程序

Use following code Based up on your condition change if else statement 使用以下代码根据条件变化(如果else语句)

int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) 
     {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
     }
 else {
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
       }

or 要么

you can set anyone landscape or portrait in your activity.its never change during the screen rotation 您可以在活动中设置任何人的风景或肖像。在屏幕旋转时它永远不会改变

<activity android:name="MyActivity"
 android:screenOrientation="landscape"
 android:configChanges="keyboardHidden|orientation|screenSize">
  ...
</activity>

In your AndroidManifest.xml, for each activity put 在您的AndroidManifest.xml中,为每个活动放置

android:screenOrientation="landscape"

It forces the activity to landscape. 它迫使活动景观。

just you have ot add following property in you manifest.xml file. 只是您在manifest.xml文件中添加了以下属性。

android:screenOrientation="portrait"

like this way, 像这样

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

That.sit 而已

You can request ScreenOrinentation public void setRequestedOrientation (int requestedOrientation) . 您可以请求ScreenOrinentation public void setRequestedOrientation (int requestedOrientation) requestOrientation public void setRequestedOrientation (int requestedOrientation) You can use it like this 你可以这样使用

// For landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 //OR for portrait 
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
//OR reverse landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
 //OR for reverse portrait
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

requestedOrientation An orientation constant as used in ActivityInfo.screenOrientation. requestOrientation ActivityInfo.screenOrientation中使用的方向常数。

Added in API level 1 The preferred screen orientation this activity would like to run in. From the screenOrientation attribute, one of SCREEN_ORIENTATION_UNSPECIFIED, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_USER, SCREEN_ORIENTATION_BEHIND, SCREEN_ORIENTATION_SENSOR, SCREEN_ORIENTATION_NOSENSOR, SCREEN_ORIENTATION_SENSOR_LANDSCAPE, SCREEN_ORIENTATION_SENSOR_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT, SCREEN_ORIENTATION_FULL_SENSOR, SCREEN_ORIENTATION_USER_LANDSCAPE, SCREEN_ORIENTATION_USER_PORTRAIT, SCREEN_ORIENTATION_FULL_USER, SCREEN_ORIENTATION_LOCKED, 在API级别1的首选屏幕取向此活动想在运行。从screenOrientation属性,SCREEN_ORIENTATION_UNSPECIFIED,SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_PORTRAIT,SCREEN_ORIENTATION_USER,SCREEN_ORIENTATION_BEHIND,SCREEN_ORIENTATION_SENSOR,SCREEN_ORIENTATION_NOSENSOR,SCREEN_ORIENTATION_SENSOR_LANDSCAPE,SCREEN_ORIENTATION_SENSOR_PORTRAIT,SCREEN_ORIENTATION_REVERSE_LANDSCAPE,SCREEN_ORIENTATION_REVERSE_PORTRAIT,SCREEN_ORIENTATION_FULL_SENSOR,SCREEN_ORIENTATION_USER_LANDSCAPE之一, SCREEN_ORIENTATION_USER_PORTRAIT,SCREEN_ORIENTATION_FULL_USER,SCREEN_ORIENTATION_LOCKED,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM