猿问
下载APP

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

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



慕运维1137616
浏览 61回答 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);

守着星空守着你

我是这样做的(灵感来自http://androidblogger.blogspot.com/2011/08/orientation-for-both-phones-and-tablets.html):在AndroidManifest.xml中,对于您希望能够在纵向和横向之间切换的每个活动(确保添加screenSize - 您以前不需要这样做!)您无需在此处设置屏幕方向。:android:configChanges="keyboardHidden|orientation|screenSize"在每个Activity中添加的方法:public static boolean isXLargeScreen(Context context) {&nbsp; &nbsp; return (context.getResources().getConfiguration().screenLayout&nbsp; &nbsp; & Configuration.SCREENLAYOUT_SIZE_MASK)&nbsp; &nbsp; >= Configuration.SCREENLAYOUT_SIZE_XLARGE;}&nbsp;和:(如果你没有覆盖这个方法,应用程序将在更改方向时调用onCreate())@Overridepublic void onConfigurationChanged (Configuration newConfig){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; super.onConfigurationChanged(newConfig);&nbsp; &nbsp; if (!isXLargeScreen(getApplicationContext()) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return; //keep in portrait mode if a phone&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; //I set background images for landscape and portrait here}在每个Activity的onCreate()中:if (!isXLargeScreen(getApplicationContext())) { //set phones to portrait;&nbsp;&nbsp; &nbsp;setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}else {&nbsp; //I set background images here depending on portrait or landscape orientation&nbsp;}我唯一无法弄清楚的是如何在从横向切换到纵向时将应用程序更改为布局文件,反之亦然。我假设答案是做与上述链接类似的事情,但我无法让它为我工作 - 它删除了我的所有数据。但是如果你有一个足够简单的应用程序,你有相同的纵向和横向布局文件,这应该工作。
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答