简体   繁体   English

如何锁定手机的屏幕方向,而不是平板电脑? (机器人)

[英]How do I lock screen orientation for phone, but not for tablet? (Android)

I have been working on a Android project, that is intended to be compatible with both the Android phones and tablets. 我一直致力于Android项目,旨在兼容Android手机和平板电脑。 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). 然而,在手机上我们已经以编程方式和布局方式遇到问题(使用10.1英寸屏幕比4英寸屏幕容易得多)。 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? 问题很简单,我如何停用Android手机的屏幕方向,同时保持Android平板电脑的活动状态? I know I can do this in the manifest file, however, this will also lock the orientation for the tablet I believe. 我知道我可以在清单文件中执行此操作,但是,这也将锁定我认为的平板电脑的方向。

You can do it from application as well. 您也可以从应用程序中执行此操作。

Lock screen orientation (Android) 锁定屏幕方向(Android)
http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int) http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

And attributes can be found here: 属性可以在这里找到:

http://developer.android.com/reference/android/R.attr.html#screenOrientation 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 要检测,如果Android设备是平板电脑或手机,您应该使用此解决方案(不同的SO问答), https://stackoverflow.com/a/9308284/492624

First off all, try to prevent doing this. 首先,尽量避免这样做。 I chose todo it because it was an easy fix for a complex problem. 我之所以选择todo,是因为它很容易解决复杂的问题。 But really, try to prevent having todo this. 但实际上,尽量避免这样做。

If you really want todo it, use the following code. 如果您真的想要它,请使用以下代码。

In your activity create a method: 在您的活动中创建一个方法:

    private boolean isTablet() {
        return (this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

And then in your onCreate do the following: 然后在你的onCreate中执行以下操作:

    if (!isTablet()) {
        // stop screen rotation on phones because <explain>
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

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. 如果你在运行时处理它,通过获取屏幕大小然后RunTimeConfigurationChanges ,它似乎并非不可能,那么它可能会对你有帮助。

Try this Handling Runtime Changes . 试试这个处理运行时更改

And let me know if you get success on it.. 如果你取得成功,请告诉我..

Thanks. 谢谢。

One method is , create separate activities for phone version and tablet version. 一种方法是为手机版和平板电脑版创建单独的活动。 And then fix their orientations in the android manifest file. 然后在android清单文件中修复它们的方向。 Another method is to check the device that's being currently used and fix the orientation in the activity itself using the code , 另一种方法是检查当前正在使用的设备,并使用代码修复活动本身的方向,

      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Creating separate activities is the better way as its easy to maintain the tab and phone versions. 创建单独的活动是更好的方式,因为它易于维护选项卡和手机版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM