我如何可以旋转显示屏只能在横向模式中的android?横向、显示屏、模式、只能在

2023-09-05 02:01:38 作者:动

32,我想查看旋转仅在横向模式下,按顺时针和逆时针方向。

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

我读到的只有逆时针为Android< 2.2这不是一个问题,我的应用程序将被+2.2现在。

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) {

和我知道如何捕捉方向

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

但总是旋转counterclocwise: - (

but always rotate to counterclocwise :-(

如何设置左,右横向?

修改的

如果我设置方位清单:

安卓screenOrientation =景观

android:screenOrientation="landscape"

活动的布局始终保持在左风景,我想左,右景观之间切换:•

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

推荐答案

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

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

安卓screenOrientation =sensorLandscape

android:screenOrientation="sensorLandscape"

和您的应用程序将转任(左或右)横向位置。

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

如果您正在构建您的应用程序为Android 2.2及以上,但要在Android 2.3及更高版本上运行它作为一个sensorLandscape的配置,你可以尝试这样的事情

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

这是处理我的情况下,横向变化的最佳方式。我是不是能找到什么更好的办法来旋转屏幕左侧或右侧横向模式为Android 2.2及以上。 我试图读取传感器的方向以及基于该横向位置,但在我看来,一旦你所说的setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_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.

顺便说一句,你真的不需要重写onConfigurationChanged(配置NEWCONFIG)的任何才能正常工作。

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