根据屏幕尺寸确定方向

根据屏幕尺寸确定方向

本文介绍了根据屏幕尺寸确定方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于手机的Android应用程序,现在我将对其进行更新以支持平板电脑大小的屏幕(尤其是Galaxy Tab 10.1).我弄清楚了整个res/layout的内容,这很好.但是,我构建的应用在AndroidManifest.xml中具有固定的屏幕方向,设置为android:screenOrientation="portrait".

I have an Android app built on a phone that I am going to update now to also support tablet-sized screens (in particular Galaxy Tab 10.1). I figured out the whole res/layout thing, so that's fine. However, I built my app to have a fixed screen orientation in AndroidManifest.xml, set as android:screenOrientation="portrait".

现在,我要进行以下操作:对于每种屏幕尺寸,都必须具有 fixed (固定)方向,一种是纵向固定(布局较小),另一种是横向固定.

Now, I want to have the following: have a fixed orientation for each screen size, the one fixed in portrait (layout-small) and the other as landscape.

使用res/layout-small-landres/layout-large-port并没有达到我想要的效果,因为它仍然从纵向切换为横向并返回.更糟糕的是,当res/layout-small-land不存在时,当我将手机旋转到横向时,应用程序崩溃.

Using res/layout-small-land and res/layout-large-port doesn't do what I want, because it still switches from portrait to landscape and back. Worse even, the app crashes when I rotate my phone to landscape, because the res/layout-small-land doesn't exist.

我可以仅通过定义XML文件来执行此操作,还是必须添加代码?

Can I do this by just defining XML files, or do I have to add code?

推荐答案

您可以使用 setRequestedOrientation .在每个活动的onCreate方法上,检查您是在平板电脑还是智能手机上,并设置所需的方向.

Instead of fixing the orientation on the manifest, you could use setRequestedOrientation. On the onCreate method of each activity, check if you are on a tablet or smartphone and set the desired orientation.

另一个选项可以是为不同的屏幕尺寸创建多个APK ,根据您的需求.

Another option could be Creating Multiple APKs for Different Screen Sizes, depending on your needs.

这篇关于根据屏幕尺寸确定方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:31