如何在 Android 中仅在横向模式下旋转显示?

发布于 2024-10-24 12:48:36 字数 871 浏览 5 评论 0原文

我希望我的视图仅在横向模式下顺时针和逆时针旋转。

我读到关于 android < 的唯一逆时针方向2.2 这不是问题,我的应用程序现在将是+2.2。

我修改我的清单以捕获配置更改

android:configChanges="keyboardHidden|orientation"

我覆盖我的活动以捕获配置更改

@Override
public void onConfigurationChanged(Configuration newConfig) {

并且我知道如何捕获方向

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int rot = display.getRotation();

但是...我不知道如何触发适当的横向方向,我正在这样做:

if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270){
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

但总是逆时针旋转:-(

如何设置左右横向方向?

编辑

如果我在清单中设置方向:

android:screenOrientation="横向"

活动的布局始终保持在“左横向”,我想在左右横向之间进行更改:S

I want that my View rotates only in landscape mode, clockwise and counterclockwise.

I read about the only counterclockwise for android < 2.2 and that's not a problem, my App will be +2.2 for now.

I modify my manifest to catch Configuration Changes

android:configChanges="keyboardHidden|orientation"

I override my activity to catch Configuration Changes

@Override
public void onConfigurationChanged(Configuration newConfig) {

and I know how to catch orientation

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int rot = display.getRotation();

but... I don't know how to trigger the appropiate landscape orientation, I am doing this:

if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270){
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

but always rotate to counterclocwise :-(

How can I set left and right landscape orientation?

EDIT

If I set orientation in manifest:

android:screenOrientation="landscape"

The activity's layout remains always in "left landscape", and I want change between left and right landscape :S

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

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

发布评论

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

评论(4

迷雾森÷林ヴ 2024-10-31 12:48:36

如果您正在为 Android 2.3 及更高版本构建应用程序,则应将清单属性设置为

android:screenOrientation="sensorLandscape"

并且您的应用程序将旋转到(左或右)横向位置。

如果您正在为 Android 2.2 及更早版本构建应用程序,但希望在 Android 2.3 及更高版本上作为“sensorLandscape”配置运行它,您可以尝试这样的操作,

public static final int ANDROID_BUILD_GINGERBREAD = 9;
public static final int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= ANDROID_BUILD_GINGERBREAD) {
        setRequestedOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }
...

这是在我的情况下处理横向方向变化的最佳方法。对于 Android 2.2 及更早版本,我无法找到任何更好的方法将屏幕旋转到左或右横向。
我尝试读取传感器方向并据此设置横向位置,但在我看来,只要您调用“setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);”传感器关闭(对于您的应用程序/活动),您无法通过传感器读取方向。

顺便说一句,您实际上并不需要覆盖“onConfigurationChanged(Configuration newConfig)”才能正常工作。

If you're building your app for Android 2.3 and newer you should set the manifest attribute as

android:screenOrientation="sensorLandscape"

and your app will rotate to either (left or right) landscape position.

If you're building your app for Android 2.2 and older but want to run it on Android 2.3 and newer as a "sensorLandscape" configuration, you could try something like this

public static final int ANDROID_BUILD_GINGERBREAD = 9;
public static final int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= ANDROID_BUILD_GINGERBREAD) {
        setRequestedOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }
...

This was the best way to handle the landscape orientation changes in my case. I was not able to find any better way to rotate the screen to left or right landscape orientations for Android 2.2 and older.
I tried reading sensor orientations and setting the landscape position based on that, but it seems to me that as soon as you call "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);" sensor turns off (for your app/activity) and you cannot read orientation through sensor.

BTW you don't really need to override the "onConfigurationChanged(Configuration newConfig)" for any of this to work properly.

战皆罪 2024-10-31 12:48:36

在清单活动声明中,不要

android:screenOrientation = "landscape"

使用

android:screenOrientation = "sensorLandscape"

In your manifest activity declaration, instead of

android:screenOrientation = "landscape"

use

android:screenOrientation = "sensorLandscape"

蛮可爱 2024-10-31 12:48:36

您是否尝试过将其添加到活动标记内的清单中,以查看它是否自动处理它?

android:screenOrientation = "landscape"

Have you tried adding this to your manifest inside the activity tag to see if it handles it automatically?

android:screenOrientation = "landscape"
抱着落日 2024-10-31 12:48:36

AndroidManifest.xml ->应用->选择您的活动 ->屏幕方向 = 横向。

AndroidManifest.xml -> Application -> selected your activity -> Screen orientation = landscape.

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