我在 VNC 查看器.org/wiki/HTC_Dream" rel="noreferrer">HTC G1。但由于某种原因,尽管我的 G1 处于纵向模式,但该应用程序始终处于横向模式。由于 Android VNC 查看器是开源的,我想知道如何将活动硬编码为“横向”。我想更改它以尊重手机方向。
I am using the Android VNC viewer 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.
发布评论
评论(16)
您可以在清单中指定活动的方向。请参阅此处。
You can specify the orientation of an activity in the manifest. See here.
在清单中:
在您的活动中:
In the manifest:
In your activity:
以下是我用来以横向模式显示所有活动的代码:
The following is the code which I used to display all activity in landscape mode:
一个快速而简单的解决方案是在 AndroidManifest.xml 文件中,为您希望强制进入横向模式的每个活动添加以下内容:
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:
这适用于 Xamarin.Android。在 OnCreate() 中
This works for Xamarin.Android. In OnCreate()
就是这样!!等待这个修复很久了。
我有一个关于双启动需要(以编程方式)横向模式的 Activity 的旧 Android 问题:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
现在,Android 在启动时设置横向模式。
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)
Now Android make Landscape mode on start.
阿尔斯兰,
为什么你想在语法上强制定向程序,尽管清单中已经有一种方法
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" />
在 Activity 中添加以下行
您需要在每个 Activity 中输入
横向
纵向
此处 MainActivity 的示例
Add The Following Lines in Activity
You need to enter in every Activity
for landscape
for portrait
Here The Example of MainActivity
在我看来,在代码中执行此操作是错误的,如果将其放入 onCreate 中,则更是如此。在清单中执行此操作,“系统”就知道应用程序启动时的方向。这种类型的元或顶级“指导”应该出现在清单中。如果您想向自己证明这一点,请在 Activity 的 onCreate 中设置一个中断。如果您在代码中执行此操作,它将被调用两次:它以纵向模式启动,然后切换到横向模式。如果您在清单中执行此操作,则不会发生这种情况。
Doing it in code is is IMO wrong and even more so if you put it into the onCreate. Do it in the manifest and the "system" knows the orientation from the startup of the app. And this type of meta or top level "guidance" SHOULD be in the manifest. If you want to prove it to yourself set a break in the Activity's onCreate. If you do it in code there it will be called twice : it starts up in Portrait mode then is switched to Landscape. This does not happen if you do it in the manifest.
对于 Android 4.0(冰淇淋三明治)及更高版本,除了
景观
值。仅使用
keyboardHidden|orientation
仍然会导致内存泄漏,并在按下电源按钮时重新启动我的活动。For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the
landscape
value.Using only
keyboardHidden|orientation
would still result in memory leaks and recreation of my activities when pressing the power button.在调用 setLayout 方法之前,在 onCreate 方法中使用 ActivityInfo (android.content.pm.ActivityInfo),如下所示
Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this
仅使用
android:screenOrientation="肖像"
工具:忽略=“LockedOrientationActivity”
use Only
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
按
CTRL+F11
旋转屏幕。Press
CTRL+F11
to rotate the screen.查看 AndroidManifest.xml (链接< /a>),第 9 行:
该行将
screenOrientation
指定为横向,但作者进一步使用configChanges="orientation|keyboardHidden"
覆盖任何屏幕方向更改。这指向 VncCanvasActivity.java 中的重写函数。如果你看一下 VncCanvasActivity,第 109 行是被重写的函数:
作者专门添加了注释以忽略任何键盘或方向更改。
如果您想更改此设置,可以返回到上面显示的 AndroidManifest.xml 文件,并将该行更改为:
这应该将程序更改为当用户旋转设备时从纵向切换到横向。
这可能有效,但可能会扰乱 GUI 的外观,具体取决于布局的创建方式。你必须考虑到这一点。此外,根据活动的编码方式,您可能会注意到,当屏幕方向更改时,填充到任何输入框中的值都会消失。这也可能必须处理。
Looking at the AndroidManifest.xml (link), on line 9:
This line specifies the
screenOrientation
as landscape, but author goes further in overriding any screen orientation changes withconfigChanges="orientation|keyboardHidden"
. This points to a overridden function in VncCanvasActivity.java.If you look at VncCanvasActivity, on line 109 is the overrided function:
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:
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.
您也可以在 Java 代码中设置相同的数据。
ActivityInfo 上的其他值可让您将其设置回传感器驱动或锁定纵向。就我个人而言,我喜欢按照此问题的另一个答案中的建议将其设置为清单中的某些内容,然后在需要时使用 Android SDK 中的上述调用进行更改。
You can set the same data in your java code as well.
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.
在我的
OnCreate(Bundle)
中,我通常执行以下操作:In my
OnCreate(Bundle)
, I generally do the following: