强制 Android Activity 始终使用横向模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2150287/
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 04:44:01  来源:igfitidea点击:

Force an Android activity to always use landscape mode

androidlandscapeandroid-orientation

提问by hap497

I am using the Android VNCviewer on my HTC G1. But for some reason, that application is always in landscape mode despite my G1 is in portrait mode. Since the Android VNC viewer is open source, I would like know how is it possible hard code an activity to be 'landscape'. I would like to change it to respect the phone orientation.

我在HTC G1上使用 Android VNC查看器。但出于某种原因,尽管我的 G1 处于纵向模式,但该应用程序始终处于横向模式。由于 Android VNC 查看器是开源的,我想知道如何将活动硬编码为“横向”。我想改变它以尊重手机方向。

回答by Pulkit Sethi

Looking at the AndroidManifest.xml (link), on line 9:

查看第 9 行的 AndroidManifest.xml(链接):

<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="VncCanvasActivity">

This line specifies the screenOrientationas landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden". This points to a overridden function in VncCanvasActivity.java.

此行将 指定screenOrientation为横向,但作者进一步使用 覆盖任何屏幕方向更改configChanges="orientation|keyboardHidden"。这指向 VncCanvasActivity.java 中的重写函数。

If you look at VncCanvasActivity, on line 109 is the overrided function:

如果您查看 VncCanvasActivity,第 109 行是被覆盖的函数:

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // ignore orientation/keyboard change
  super.onConfigurationChanged(newConfig);
}

The author specifically put a comment to ignore any keyboard or orientation changes.

作者专门发表评论以忽略任何键盘或方向更改。



If you want to change this, you can go back to the AndroidManifest.xml file shown above, and change the line to:

如果你想改变这一点,你可以回到上面显示的 AndroidManifest.xml 文件,并将该行更改为:

<activity android:screenOrientation="sensor" android:name="VncCanvasActivity">

This should change the program to switch from portrait to landscape when the user rotates the device.

当用户旋转设备时,这应该会改变程序从纵向切换到横向。

This may work, but might mess up how the GUI looks, depending on how the layout were created. You will have to account for that. Also, depending on how the activities are coded, you may notice that when screen orientation is changed, the values that were filled into any input boxes disappear. This also may have to be handled.

这可能有效,但可能会弄乱 GUI 的外观,具体取决于布局的创建方式。你将不得不考虑到这一点。此外,根据活动的编码方式,您可能会注意到,当屏幕方向改变时,填充到任何输入框中的值都会消失。这也可能需要处理。

回答by haseman

You can set the same data in your java code as well.

您也可以在 Java 代码中设置相同的数据。

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Other values on ActivityInfo will let you set it back to sensor driven or locked portrait. Personally, I like to set it to something in the Manifest as suggested in another answer to this question and then change it later using the above call in the Android SDK if there's a need.

ActivityInfo 上的其他值将让您将其设置回传感器驱动或锁定肖像。就我个人而言,我喜欢按照此问题的另一个答案中的建议将其设置为 Manifest 中的某些内容,然后在需要时使用 Android SDK 中的上述调用进行更改。

回答by anwarma

In my OnCreate(Bundle), I generally do the following:

在 my 中OnCreate(Bundle),我通常会执行以下操作:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

回答by Michael Krauklis

You can specify the orientation of an activity in the manifest. See here.

您可以在清单中指定活动的方向。见这里

<activity android:allowTaskReparenting=["true" | "false"]
...
          android:screenOrientation=["unspecified" | "user" | "behind" |
                                     "landscape" | "portrait" |
                                     "sensor" | "nosensor"]
...
                                       "adjustResize", "adjustPan"] >  

回答by avisper

In the manifest:

在清单中:

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

In your activity:

在您的活动中:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.your_activity_layout);

回答by Megha

The following is the code which I used to display all activity in landscape mode:

以下是我用来以横向模式显示所有活动的代码:

<activity android:screenOrientation="landscape"
          android:configChanges="orientation|keyboardHidden"
          android:name="abcActivty"/>

回答by joshgoldeneagle

A quick and simple solution is for the AndroidManifest.xml file, add the following for each activity that you wish to force to landscape mode:

一个快速而简单的解决方案是针对 AndroidManifest.xml 文件,为您希望强制为横向模式的每个活动添加以下内容:

android:screenOrientation="landscape"

回答by ComeIn

This works for Xamarin.Android. In OnCreate()

这适用于 Xamarin.Android。在 OnCreate()

RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;

回答by AndrewDC

That's it!! Long waiting for this fix.

就是这样!!等这个修复好久了。

I've an old Android issue about double-start an activity that required (programmatically) landscape mode: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

我有一个关于双重启动需要(以编程方式)横向模式的活动的旧 Android 问题: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Now Android make Landscape mode on start.

现在Android在启动时制作横向模式。

回答by Blue Moon

Arslan, why do you want to force orientation pro grammatically, though there's already a way in manifest <activity android:name=".youractivityName" android:screenOrientation="portrait" />

Arslan,你为什么要以编程方式强制定向,尽管清单中已经有方法 <activity android:name=".youractivityName" android:screenOrientation="portrait" />