在Android中禁用横向模式?

los*_*sit 910 android android-manifest android-orientation

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

Yon*_*lan 1586

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

<activity android:name=".SomeActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" />
Run Code Online (Sandbox Code Playgroud)

编辑:由于这已成为一个超级流行的答案,我感到非常内疚,因为强迫肖像很少是解决它经常应用的问题的正确方法.
强迫肖像的主要警告:

  • 这并不能免除您必须考虑活动生命周期事件或正确保存/恢复状态.除了应用程序轮换之外,还有很多东西可以触发活动破坏/娱乐,包括不可避免的事情,如多任务处理.没有捷径; 学会使用捆绑和retainInstance碎片.
  • 请记住,与相当统一的iPhone体验不同,有许多设备,其中肖像不是明显受欢迎的方向.当用户使用带有硬件键盘或游戏手柄的设备(包括Droid 1-4,Xperia Play或Nvidia Shield)时,强制纵向可以使您的应用程序成为一个巨大的可用性麻烦(特别是在盾牌上).如果您的应用程序没有非常具体的用例,这会导致支持其他方向的直接负面体验,那么您可能不应该强迫横向.我说的是"这是一款遥控器应用程序,用于仅在设备的一侧有IR发射器的手机",或"这是一个收银机应用程序,适用于固定使用的一种特定型号的平板电脑硬件码头."

因此,大多数应用应该让手机传感器,软件和物理配置自行决定用户如何与您的应用进行互动.但是,如果您对sensor用例中的方向的默认行为不满意,可能还需要考虑一些案例:

  • 如果您的主要关注点是意外方向改变,您认为设备的传感器和软件无法很好地应对(例如,在基于倾斜的游戏中),请考虑支持横向和纵向,但使用nosensor方向.大多数手机上的大多数平板电脑和人像都有这种风景,但我仍然不建议大多数"正常"的应用程序(有些用户喜欢在手机上键入横向软键盘,许多平板电脑用户都是纵向阅读 - 以及你应该让他们).
  • 如果由于某种原因你仍然需要强制肖像,sensorPortrait可能比portraitAndroid 2.3+ 更好; 这允许颠倒的肖像,这在平板电脑使用中非常常见.

  • 可以为整个应用程序执行此操作.请查看http://stackoverflow.com/a/9784269/1300707 (52认同)
  • 正如我在下面的回答中提到的 - 要解决几个问题,"nosensor"可能是更好的选择. (2认同)

Ric*_*ich 105

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

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);     //  Fixed Portrait orientation
Run Code Online (Sandbox Code Playgroud)

  • 如果设备未处于指定方向,这可以使您的活动在首次加载时跳转. (8认同)

Dee*_*ami 40

android:screenOrientation="portrait"在清单文件中添加此项,您可以在此处声明您的活动

<activity android:name=".yourActivity"
    ....
    android:screenOrientation="portrait" />
Run Code Online (Sandbox Code Playgroud)

如果你想用java代码试试

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
Run Code Online (Sandbox Code Playgroud)

setContentView为您的活动调用方法之前onCreate().

希望这对所有人都有帮助并且易于理解......


Mik*_*eir 25

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

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


小智 12

如果你想要用户设置,

那我建议 setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

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

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


Sun*_*ary 12

只需在你的清单中添加Like this Line

机器人:screenOrientation = "画像"

<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>   
Run Code Online (Sandbox Code Playgroud)


Bri*_*hod 12

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

诀窍是首先确保您在项目中包含一个 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);
            }
            ....
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Kar*_*ani 7

在Activity的onCreate()中使用它

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)


Mar*_*rci 7

将其放入您的清单中。

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="sensorPortrait" />
Run Code Online (Sandbox Code Playgroud)

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


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

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


Dee*_*rma 6

只需在您的活动代码中添加此属性即可.

 android:screenOrientation="portrait"
Run Code Online (Sandbox Code Playgroud)


vuh*_*990 6

你应该android:screenOrientation="sensorPortrait"在AndroidManifest.xml中进行更改


alc*_*ist 6

如果您不想更好地处理在每个清单活动条目中添加方向的麻烦,请创建一个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......
}
Run Code Online (Sandbox Code Playgroud)


Swa*_*yam 5

用:

android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait" 
Run Code Online (Sandbox Code Playgroud)


Zhe*_*Hao 5

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


Aks*_*wal 5

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

<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>
Run Code Online (Sandbox Code Playgroud)


Int*_*iya 5

如果您要禁用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">
Run Code Online (Sandbox Code Playgroud)

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

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)

希望对您有所帮助。有关更多详细信息,请访问此处,在此处输入链接说明

  • 由于这么多答案给出的建议与@Phoenix 和@Yoni 的好建议相矛盾,我认为一个好的底线是重申他们的建议:`android:screenOrientation="nosensor"&gt;` (2认同)

小智 5

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

[Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
Run Code Online (Sandbox Code Playgroud)

以上类效果很好,类似于其他解决方案。此外,它不是全局适用的,需要放置在每个活动标题中。


Hit*_*ahu 5

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

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

使您的风景

OrientationUtils.lockOrientationLandscape(mActivity);
Run Code Online (Sandbox Code Playgroud)

使您的视图人像

OrientationUtils.lockOrientationPortrait(mActivity);
Run Code Online (Sandbox Code Playgroud)

解锁方向

OrientationUtils.unlockOrientation(mActivity);
Run Code Online (Sandbox Code Playgroud)

方向实用程序类

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

}
Run Code Online (Sandbox Code Playgroud)