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

发布于 2024-08-19 08:58:47 字数 357 浏览 5 评论 0 原文

我在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

贪了杯 2024-08-26 08:58:48

您可以在清单中指定活动的方向。请参阅此处

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

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"] >  
挽手叙旧 2024-08-26 08:58:48

在清单中:

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

在您的活动中:

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

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);
捶死心动 2024-08-26 08:58:48

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

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

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"/>
黒涩兲箜 2024-08-26 08:58:48

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

android:screenOrientation="landscape"

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:

android:screenOrientation="landscape"
捂风挽笑 2024-08-26 08:58:48

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

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

This works for Xamarin.Android. In OnCreate()

RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
少钕鈤記 2024-08-26 08:58:48

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

我有一个关于双启动需要(以编程方式)横向模式的 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.

指尖上得阳光 2024-08-26 08:58:48

阿尔斯兰,
为什么你想在语法上强制定向程序,尽管清单中已经有一种方法

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

故事灯 2024-08-26 08:58:48

在 Activity 中添加以下行

您需要在每个 Activity 中输入

横向

android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"

纵向

android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"

此处 MainActivity 的示例

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.thcb.app">
    <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="landscape"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity2"
            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>
    </application>
</manifest>

Add The Following Lines in Activity

You need to enter in every Activity

for landscape

android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"

for portrait

android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"

Here The Example of MainActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.thcb.app">
    <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="landscape"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity2"
            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>
    </application>
</manifest>
乱世争霸 2024-08-26 08:58:48

在我看来,在代码中执行此操作是错误的,如果将其放入 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.

红尘作伴 2024-08-26 08:58:48

对于 Android 4.0(冰淇淋三明治)及更高版本,除了 景观值。

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

仅使用 keyboardHidden|orientation 仍然会导致内存泄漏,并在按下电源按钮时重新启动我的活动。

For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the landscape value.

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

Using only keyboardHidden|orientation would still result in memory leaks and recreation of my activities when pressing the power button.

安静被遗忘 2024-08-26 08:58:48

在调用 setLayout 方法之前,在 onCreate 方法中使用 ActivityInfo (android.content.pm.ActivityInfo),如下所示

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
最冷一天 2024-08-26 08:58:48

仅使用
android:screenOrientation="肖像"
工具:忽略=“LockedOrientationActivity”

use Only
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"

衣神在巴黎 2024-08-26 08:58:48

CTRL+F11 旋转屏幕。

Press CTRL+F11 to rotate the screen.

情域 2024-08-26 08:58:47

查看 AndroidManifest.xml (链接< /a>),第 9 行:

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

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

如果你看一下 VncCanvasActivity,第 109 行是被重写的函数:

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

作者专门添加了注释以忽略任何键盘或方向更改。


如果您想更改此设置,可以返回到上面显示的 AndroidManifest.xml 文件,并将该行更改为:

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

这应该将程序更改为当用户旋转设备时从纵向切换到横向。

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

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

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

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

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

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

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

南烟 2024-08-26 08:58:47

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

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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

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

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.

用心笑 2024-08-26 08:58:47

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

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文