398

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

4

8 回答 8

854

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

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

让我解释:

  • 告诉Android android:configChanges="orientation",您将负责方向的变化。
  • android:screenOrientation="portrait"您设置默认方向模式。
于 2010-09-16T04:54:41.910 回答
64

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

于 2010-09-16T04:55:50.957 回答
27

有两种方式,

  1. android:screenOrientation="portrait"为清单文件中的每个活动添加
  2. 添加this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);每个java文件。
于 2013-12-24T06:02:39.203 回答
10

在清单中:

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

或:在 MainActivity

@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
于 2020-03-27T07:37:27.257 回答
3

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

   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); }

    }

奇迹般有效!

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

于 2014-03-13T10:33:26.307 回答
0

new SDkandroid中,(24 and above)您可以根据需要使用Manifest.xmlinactivity标记:

<activity
            android:name=".feature.main.MainActivity"
            ********* android:screenOrientation="locked" ******
            android:configChanges="uiMode"
            android:windowSoftInputMode="adjustPan"
            android:exported="true">

它工作!

于 2021-11-17T11:11:07.033 回答
0

试试这个:(如果 SDK 23 及更高版本)

添加你的AndroidManifest.xlm;

    <activity android:name=".YourActivity"
        android:screenOrientation="locked"/>

像这样。

于 2021-08-04T14:45:07.627 回答
-6

我用

 android:screenOrientation="nosensor"

如果您不想支持上下纵向模式,这将很有帮助。

于 2013-12-22T05:45:24.600 回答