猿问
下载APP

Android:允许平板电脑的纵向和横向,但强制手机上的肖像?

我希望平板电脑能够以纵向和横向(sw600dp或更高)显示,但手机仅限于纵向。我找不到有条件选择方向的方法。有什么建议么?



慕运维1137616
浏览 188回答 3
3回答

回首忆惘然

这是使用资源和大小限定符的好方法。将此bool资源放在res / values中作为bools.xml或其他任何内容(文件名在这里无关紧要):&nbsp; &nbsp; <?xml version="1.0" encoding="utf-8"?>&nbsp; &nbsp; <resources>&nbsp; &nbsp; &nbsp; &nbsp; <bool name="portrait_only">true</bool>&nbsp; &nbsp; </resources>把这个放在res / values-sw600dp和res / values-xlarge中:&nbsp; &nbsp; <?xml version="1.0" encoding="utf-8"?>&nbsp; &nbsp; <resources>&nbsp; &nbsp; &nbsp; &nbsp; <bool name="portrait_only">false</bool>&nbsp; &nbsp; </resources>有关在Android Studio中添加这些目录和文件的帮助,请参阅此补充答案。然后,在您的活动的onCreate方法中,您可以这样做:&nbsp; &nbsp; if(getResources().getBoolean(R.bool.portrait_only)){&nbsp; &nbsp; &nbsp; &nbsp; setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);&nbsp; &nbsp; }在最小宽度方向上超过600 dp的设备,或在Android 3.2之前的设备(平板电脑,基本上)上的x-large将基于传感器和用户锁定旋转等行为正常。其他一切(电话,几乎)只会是肖像。

拉风的咖菲猫

您可以尝试这种方式首先获取设备的屏幕尺寸if ((getResources().getConfiguration().screenLayout &&nbsp; &nbsp; &nbsp; Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();}else if ((getResources().getConfiguration().screenLayout &&nbsp; &nbsp; &nbsp; Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();}&nbsp;else if ((getResources().getConfiguration().screenLayout &&nbsp; &nbsp; &nbsp; Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();}else {&nbsp; &nbsp; Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();}然后根据它设置方向setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答