如何在 Android 中仅在横向模式下旋转显示?
我希望我的视图仅在横向模式下顺时针和逆时针旋转。
我读到关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在为 Android 2.3 及更高版本构建应用程序,则应将清单属性设置为
并且您的应用程序将旋转到(左或右)横向位置。
如果您正在为 Android 2.2 及更早版本构建应用程序,但希望在 Android 2.3 及更高版本上作为“sensorLandscape”配置运行它,您可以尝试这样的操作,
这是在我的情况下处理横向方向变化的最佳方法。对于 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
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
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.
在清单活动声明中,不要
使用
android:screenOrientation = "sensorLandscape"
In your manifest activity declaration, instead of
use
android:screenOrientation = "sensorLandscape"
您是否尝试过将其添加到活动标记内的清单中,以查看它是否自动处理它?
Have you tried adding this to your manifest inside the activity tag to see if it handles it automatically?
AndroidManifest.xml ->应用->选择您的活动 ->屏幕方向 = 横向。
AndroidManifest.xml -> Application -> selected your activity -> Screen orientation = landscape.