如何锁定手机而非平板电脑的屏幕方向? (安卓)
我一直在开发一个 Android 项目,旨在与 Android 手机和平板电脑兼容。在平板电脑上,该应用程序运行良好,而且看起来也不错。然而,在手机上,我们在编程和布局方面都遇到了问题(使用 10.1 英寸屏幕比使用 4 英寸屏幕容易得多)。为了解决其中一些问题,我们决定停用屏幕方向,但仅限于手机版本,至少暂时如此。
问题很简单,如何停用 Android 手机的屏幕方向,同时保持 Android 平板电脑的屏幕方向处于活动状态?我知道我可以在清单文件中执行此操作,但是,这也会锁定我认为的平板电脑的方向。
I have been working on a Android project, that is intended to be compatible with both the Android phones and tablets. On the tablet the app is working great and looks good too. However, on the phone we have been running into issues both programmatically and layout wise (working with a 10.1 inch screen is a lot easier than a 4 inch screen). To solve some of these problems we have decided to deactivate screen orientation but only for the phone version, atleast temporarily.
The question is simple, how do I deactivate the screen orientation for Android phone, while keeping it active for the Android tablets? I know I can do this in the manifest file, however, this will also lock the orientation for the tablet I believe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,尽量避免这样做。我选择这样做是因为它可以轻松解决复杂问题。但实际上,尽量避免这样做。
如果您确实想这样做,请使用以下代码。
在您的活动中创建一个方法:
然后在您的 onCreate 中执行以下操作:
First off all, try to prevent doing this. I chose todo it because it was an easy fix for a complex problem. But really, try to prevent having todo this.
If you really want todo it, use the following code.
In your activity create a method:
And then in your onCreate do the following:
您也可以从应用程序中执行此操作。
锁定屏幕方向 (Android)
http://developer.android.com/reference/ android/app/Activity.html#setRequestedOrientation(int)
属性可以在这里找到:
http://developer.android.com/reference/android/R.attr。 html#screenOrientation
要检测 Android 设备是平板电脑还是手机,您应该使用此解决方案(不同的 SO Q&A),https://stackoverflow.com/a/9308284/492624
You can do it from application as well.
Lock screen orientation (Android)
http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)
And attributes can be found here:
http://developer.android.com/reference/android/R.attr.html#screenOrientation
To detect, if Android device is Tablet or Phone, you should use this solution (different SO Q&A), https://stackoverflow.com/a/9308284/492624
如果您在运行时处理这个问题,通过获取屏幕尺寸然后进行 RunTimeConfigurationChanges ,这似乎并非不可能,那么它可能会对您有所帮助。
尝试处理运行时更改。
如果你成功了请告诉我......
谢谢。
Its seem like, not impossible if you handling this at runtime, by getting screen size and then make
RunTimeConfigurationChanges
, Then may it will be help you.Try this Handling Runtime Changes.
And let me know if you get success on it..
Thanks.
一种方法是,为手机版本和平板电脑版本创建单独的活动。然后在 android 清单文件中修复它们的方向。另一种方法是检查当前正在使用的设备并使用代码修复活动本身的方向,
创建单独的活动是更好的方法,因为它很容易维护选项卡和手机版本。
One method is , create separate activities for phone version and tablet version. And then fix their orientations in the android manifest file. Another method is to check the device that's being currently used and fix the orientation in the activity itself using the code ,
Creating separate activities is the better way as its easy to maintain the tab and phone versions.