我希望我的 android 应用程序只能在纵向模式下运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3723823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:32:14  来源:igfitidea点击:

I want my android application to be only run in portrait mode?

androidscreen-orientation

提问by James

I want my android application to be only run in portrait mode? How can I do that?

我希望我的 android 应用程序只能在纵向模式下运行?我怎样才能做到这一点?

回答by Cristian

In the manifest, set this for all your activities:

在清单中,为您的所有活动设置此项:

<activity android:name=".YourActivity"
    android:configChanges="orientation"
    android:screenOrientation="portrait"/>

Let me explain:

让我解释:

  • With android:configChanges="orientation"you tell Android that you will be responsible of the changes of orientation.
  • android:screenOrientation="portrait"you set the default orientation mode.
  • 随着android:configChanges="orientation"你告诉的Android,你会负责的取向的变化。
  • android:screenOrientation="portrait"您设置默认方向模式。

回答by Praveen

In Android Manifest File, put attribute for your <activity>that android:screenOrientation="portrait"

在 Android 清单文件中,为您的<activity>那个放置属性android:screenOrientation="portrait"

回答by Android Code Solution

There are two ways,

有两种方式,

  1. Add android:screenOrientation="portrait"for each Activity in Manifest File
  2. Add this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);in each java file.
  1. android:screenOrientation="portrait"为清单文件中的每个活动添加
  2. 添加this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);每个java文件。

回答by Meysam Keshvari

in the manifest:

在清单中:

<activity android:name=".activity.MainActivity"
        android:screenOrientation="portrait"
        tools:ignore="LockedOrientationActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

or : in the MainActivity

或:在 MainActivity 中

@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

回答by Codebeat

Old post I know. In order to run your app always in portrait mode even when orientation may be or is swapped etc (for example on tablets) I designed this function that is used to set the device in the right orientation without the need to know how the portrait and landscape features are organised on the device.

我知道的旧帖子。为了使您的应用始终以纵向模式运行,即使方向可能或已交换等(例如在平板电脑上),我设计了此功能,用于将设备设置为正确的方向,而无需知道纵向和横向如何功能组织在设备上。

   private void initActivityScreenOrientPortrait()
    {
        // Avoid screen rotations (use the manifests android:screenOrientation setting)
        // Set this to nosensor or potrait

        // Set window fullscreen
        this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        DisplayMetrics metrics = new DisplayMetrics();
        this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

         // Test if it is VISUAL in portrait mode by simply checking it's size
        boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); 

        if( !bIsVisualPortrait )
        { 
            // Swap the orientation to match the VISUAL portrait mode
            if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
             { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
            else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
        }
        else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }

    }

Works like a charm!

奇迹般有效!

NOTICE: Change this.activityby your activity or add it to the main activity and remove this.activity;-)

注意:this.activity根据您的活动更改或将其添加到主要活动并删除this.activity;-)

回答by user2305886

I use

我用

 android:screenOrientation="nosensor"

It is helpful if you do not want to support up side down portrait mode.

如果您不想支持倒置纵向模式,这会很有帮助。