ChatGPT解决这个技术问题 Extra ChatGPT

如何在 Android 中禁用横向模式?

如何为我的 Android 应用程序中的某些视图禁用横向模式?

您是否实际上是在尝试禁用自动旋转功能。尝试在纵向视图中设置首选方向? http://code.google.com/android/reference/android/R.styleable.html#AndroidManifestActivity_screenOrientation

P
Peter Mortensen

android:screenOrientation="portrait" 添加到 AndroidManifest.xml 中的 Activity。例如:

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

由于这已成为一个超级流行的答案,我感到非常内疚,因为强制肖像很少是解决它经常应用的问题的正确解决方案。强制肖像的主要注意事项:

这并不能免除您必须考虑活动生命周期事件或正确保存/恢复状态的责任。除了应用程序轮换之外,还有很多事情可以触发活动破坏/重新创建,包括不可避免的事情,例如多任务处理。没有捷径;学习使用 bundles 和 retainInstance 片段。

请记住,与相当统一的 iPhone 体验不同,有些设备的纵向并不是明显流行的方向。当用户使用带有硬件键盘或游戏手柄的设备(如 Nvidia Shield、Chromebook、可折叠设备或三星 DeX)时,强制纵向可能会使您的应用体验受到限制或极大的可用性麻烦。如果您的应用程序没有强大的 UX 参数会导致支持其他方向的负面体验,那么您可能不应该强制横向。我说的是“这是一款收银机应用程序,适用于始终用于固定硬件底座的特定型号平板电脑”。

因此,大多数应用程序应该让手机传感器、软件和物理配置自行决定用户希望如何与您的应用程序交互。但是,如果您对用例中 sensor 方向的默认行为不满意,您可能仍需要考虑以下几种情况:

如果您主要担心的是在您认为设备的传感器和软件无法很好应对的活动过程中意外的方向变化(例如,在基于倾斜的游戏中),请考虑支持横向和纵向,但使用 nosensor 作为方向。这会在大多数平板电脑上强制使用横向,在大多数手机上强制使用纵向,但我仍然不建议对大多数“普通”应用程序这样做(有些用户只是喜欢在手机上输入横向软键盘,而许多平板电脑用户以纵向阅读 - 和你应该让他们)。

如果由于某种原因仍需要强制人像,则对于 Android 2.3 (Gingerbread) 及更高版本,sensorPortrait 可能比 Portrait 更好;这允许倒置的肖像,这在平板电脑使用中很常见。


可以为整个应用程序执行此操作。看看这个stackoverflow.com/a/9784269/1300707
我注意到还有另一幅肖像:sensorPortait。 sensorPortait 和 Portrait 有什么区别?
如果您阅读 Google 的 docs:“纵向,但可以是基于设备传感器的正常或反向纵向。在 API 级别 9 中添加。”所以 - 那是 - “肖像,正面朝上或倒置,仅限 Android 2.3+。”
正如我在下面的回答中指出的那样 - 为了解决几个问题,“nosensor”可能是一个更好的选择。
我不得不说我不同意。并非所有体验都与手机上的横向模式兼容,因此您应该让用户体验决定,而不是传感器。但是,平板电脑并非如此。
P
Peter Mortensen

在阅读这篇文章之前,我不知道 AndroidManifest.xml 文件切换,所以在我的应用程序中我使用了这个:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed portrait orientation

如果设备不在指定的方向,这可以使您的活动在第一次加载时跳转。
我正在使用这种方法,我在 OnCreate 中调用它,然后我会从一些资产文件中读取数据。如果我用横向设备启动应用程序,它会旋转,但这会导致错误地读取这些初始化资产,出于某种奇怪的原因(也许应该等待旋转完成某些方式)。使用 xml 替代方案不会导致此问题。
这可能是 Android 程序启动顺序的问题。您可以尝试将 setRequestedOrientation() 移动到 onResume() ???
P
Peter Mortensen

在您声明活动的清单文件中添加 android:screenOrientation="portrait"。像这样:

<activity 
    android:name=".yourActivity"
    ....
    android:screenOrientation="portrait" />

如果您想使用 Java 代码执行此操作,请尝试:

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

在您为 onCreate() 中的活动调用 setContentView 方法之前。


M
Mike Weir

这里的很多答案都建议在您的 AndroidManifest.xml 文件中使用 "portrait"。这似乎是一个很好的解决方案 - 但如文档中所述,您正在挑选可能只有景观的设备。您还强制某些设备(在横向上效果最好)进入纵向,而不是获得正确的方向。

我的建议是改用 "nosensor"。这将使设备使用其默认的首选方向,不会阻止 Google Play 上的任何购买/下载,并确保传感器不会弄乱您的(在我的情况下为 NDK)游戏。


由于许多人对此表示赞同,我想指出,我已经接受了其他建议“纵向”的答案,因为某些不起眼的设备实际上在想要实现应用内屏幕旋转时做出不可预测的响应,这成为了以后对我的要求。我也怀疑任何带有“肖像”的应用程序都会被任何 Google Play 设置阻止。
Google Play 实际上会过滤掉仅横向设备上的仅纵向应用程序,请参阅此页面上的注释:developer.android.com/guide/topics/manifest/…(“您声明的值允许按 Google Play 等服务进行过滤”)
I
IntelliJ Amiya

如果您想禁用 Landscape mode for your Android app(或单个活动),您只需添加:

android:screenOrientation="portrait"AndroidManifest.xml 文件中的活动标记。

喜欢:

<activity 
    android:name="YourActivityName"
    android:icon="@drawable/ic_launcher"
    android:label="Your App Name"
    android:screenOrientation="portrait">

另一种方式:程序化方法。

如果您想以编程方式执行此操作,即使用 Java 代码。您可以通过在您不希望以横向模式显示的活动的 Java 类中添加以下代码来实现。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

由于这么多答案给出的建议与@Phoenix 和@Yoni 的好建议相矛盾,我认为一个好的底线是重申他们的建议:android:screenOrientation="nosensor">
@DSlomer64 是的。如果你想处理平板设备,那么你应该使用 nosensor 值而不是 Portrait
P
Peter Mortensen

只需在您的清单中添加这一行:

android:screenOrientation="portrait"

喜欢:

<manifest
    package="com.example.speedtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

    </application>

</manifest>

P
Peter Mortensen

如果您需要用户设置,那么我建议您使用 setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

您可以从设置菜单更改设置。

我需要这个,因为我的计时器必须与屏幕上的内容相对应,旋转屏幕会破坏当前的活动。


A
Alp Altunel

您可以对整个应用程序执行此操作,而不必让所有活动都扩展一个公共基类。

诀窍是首先确保在项目中包含一个 Application 子类。在您的应用程序首次启动时调用的 onCreate() 中,您注册一个 ActivityLifecycleCallbacks 对象(API 级别 14+)以接收活动生命周期事件的通知。

这使您有机会在应用程序中的任何活动开始(或停止,或恢复,或其他)时执行自己的代码。此时,您可以对新创建的活动调用 setRequestedOrientation()。

并且不要忘记在清单文件中添加 app:name=".MyApp"。

class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();  

        // register to be informed of activities starting up
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, 
                                          Bundle savedInstanceState) {

                // new activity created; force its orientation to portrait
                activity.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            ....
        });
    }
}

K
Karan Datwani

在 Activity 的 onCreate() 中使用它

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

V
Vishal Yadav

您应该更改 AndroidManifest.xml 中的 android:screenOrientation="sensorPortrait"


改成什么?
D
Deepak Sharma

只需在您的活动标签中添加此属性即可。

 android:screenOrientation="portrait"

鉴于之前的答案,为什么这么简单?例如,它是否取决于 Android 版本?
a
alchemist

如果您不想更好地在活动的每个清单条目中添加方向的麻烦,请创建一个 BaseActivity 类(继承“Activity”或“AppCompatActivity”),它将被应用程序的每个活动而不是“Activity”继承' 或 'AppCompatActivity' 并在您的 BaseActivity 中添加以下代码:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // rest of your code......
}

实现继承?
P
Peter Mortensen

android:screenOrientation="portrait" 添加到要禁用横向模式的 Activity。


P
Peter Mortensen

如何在某些视图中更改方向

除了锁定整个活动的方向,您可以使用此类从任何视图中动态地锁定方向,实用:

让你的视野风景

OrientationUtils.lockOrientationLandscape(mActivity);

使您的视图纵向

OrientationUtils.lockOrientationPortrait(mActivity);

解锁方向

OrientationUtils.unlockOrientation(mActivity);

方向实用程序类

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Surface;
import android.view.WindowManager;

/*  * This class is used to lock orientation of android app in nay android devices
 */

public class OrientationUtils {
    private OrientationUtils() {
    }

    /** Locks the device window in landscape mode. */
    public static void lockOrientationLandscape(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }

    /** Locks the device window in portrait mode. */
    public static void lockOrientationPortrait(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /** Locks the device window in actual screen mode. */
    public static void lockOrientation(Activity activity) {
        final int orientation = activity.getResources().getConfiguration().orientation;
        final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getRotation();

        // Copied from Android docs, since we don't have these values in Froyo
        // 2.2
        int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
        int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;

        // Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
        if (!(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)) {
            SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }

        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        } else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }
    }

    /** Unlocks the device window in user defined screen mode. */
    public static void unlockOrientation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    }

}

关于“务实”:你的意思是programmatically(不是反问句)吗?
P
Peter Mortensen

利用:

android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait" 

一个解释将是有序的。例如,想法/要点是什么?它与以前的答案有什么不同/更好?
P
Peter Mortensen

您必须设置每个活动的方向。

<activity
    android:name="com.example.SplashScreen2"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.Registration"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.Verification"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.WelcomeAlmostDone"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
    android:name="com.example.PasswordRegistration"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>

P
Peter Mortensen

如果您使用的是 Xamarin C#,其中一些解决方案将不起作用。这是我发现可行的解决方案。

[Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]

上面的类运行良好,类似于其他解决方案。此外,它不是全局适用的,需要放在每个活动标题中。


您所说的“高于班级运作良好”是什么意思?
M
Marci

把它放到你的清单中。

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

方向将是纵向的,但如果用户的手机是倒置的,它也会显示正确的方向。 (所以你的屏幕会旋转 180 度。)

如果活动在多窗口模式下运行,系统将忽略此属性。

更多:https://developer.android.com/guide/topics/manifest/activity-element


重新“这个属性”:什么属性?它指的是什么?
“此属性”指的是“screenOreintation”属性
P
Peter Mortensen

在 oncreate() 方法中添加一个类:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

那如何添加一个类?
P
Peter Mortensen

您可以通过在 manifest.xml 文件中写入以下内容来强制您的特定活动始终保持纵向模式:

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"></activity>

您还可以通过在活动的 onCreate() 方法中编写以下行来强制活动保持纵向模式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

P
Peter Mortensen
<android . . . >
    . . .
    <manifest . . . >
        . . .
        <application>
            <activity
                android:name=".MyActivity"
                android:screenOrientation="portrait"
                android:configChanges="keyboardHidden|orientation">
            </activity>
        </application>
    </manifest>
</android>

P
Peter Mortensen
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.co.nurture.bajajfinserv">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我们可以使用属性或 android:screenOrientation 将 Activity 限制为纵向或横向模式。

如果我们的程序中有多个活动,那么我们可以自由地以任何一种模式限制任何一项活动,并且它永远不会影响您不想要的其他活动。


P
Peter Mortensen

在清单类中:

<activity android:name=".yourActivity"
    ....
    android:screenOrientation="portrait" />

或以编程方式:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

注意:您应该在 onCreate() 中的活动的 setContentView 方法之前调用此方法。


@PeterMortensen 按 ctrl+N,然后输入 manifest
P
Peter Mortensen

将以下命令添加到您的项目中,

npm install

npm i react-native-orientation-locker

然后使用清单类,例如 React_Native(您的项目文件夹)/android/app/src/main/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.payroll_react">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

  <application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:allowBackup="false"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:screenOrientation="landscape"
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
      android:windowSoftInputMode="adjustResize">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
  </application>

</manifest>

P
Peter Mortensen

<apphome>/platform/android 目录中,创建 AndroidManifest.xml(从生成的目录中复制它)。

然后将 android:screenOrientation="portrait" 添加到 所有 活动元素。


P
Peter Mortensen

AndroidManifest.xml 文件中添加 android:screenOrientation="portrait"

例如:

<activity 
    android:name=".MapScreen"
    android:screenOrientation="portrait"></activity>

P
Peter Mortensen

它对我有用。尝试在 AndroidManifest 文件中添加此代码:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme">
    ....
    ....
</application>

我本来是一个很好的解决方案,因为您不必为每个活动设置。不幸的是,在我的应用程序中不起作用。
“AndroidManifest”是文件的(字面)名称吗?
P
Peter Mortensen

AndroidManifest.xml 中 Activity 的以下属性就是您所需要的:

android:configChanges="orientation"

所以,完整的活动节点:

<activity
    android:name="Activity1"
    android:icon="@drawable/icon"
    android:label="App Name"
    android:configChanges="orientation">

剩下的答案太多了,您应该提供更多信息,为什么要使用您的答案。特别是文档说不使用此选项:“注意:应避免使用此属性,并且仅将其用作最后的手段。请阅读处理运行时更改以获取有关如何正确处理由于配置更改而重新启动的更多信息。” developer.android.com/guide/topics/manifest/…
P
Peter Mortensen

Kotlin 中,同样可以使用以下代码以编程方式实现:

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

P
Peter Mortensen

如果您的 Activity 与第一个设备方向状态相关,请在 onCreate 方法中获取当前设备方向,然后永久修复它:

int deviceRotation = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

if(deviceRotation == Surface.ROTATION_0) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_180)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_90)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(deviceRotation == Surface.ROTATION_270)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}