简体   繁体   English

Android-如何在xml配置中为不同的屏幕尺寸锁定不同的屏幕方向

[英]Android - How can I lock different screen orientation for different screen sizes in xml configuration

Is there a way to lock different screen orientation for different screen sizes? 有没有一种方法可以针对不同的屏幕尺寸锁定不同的屏幕方向? So for screens with smallest width smaller than 600 dp to lock portrait orientation and for screens with smallest width greater than 600 dp to lock landscape orientation? 那么对于最小宽度小于600 dp的屏幕可以锁定纵向,对于最小宽度大于600 dp的屏幕可以锁定横向吗? I understand that this can be done programmatically with setRequestedOrientation , but can it be done in xml resources somehow? 我知道可以使用setRequestedOrientation以编程方式完成setRequestedOrientation ,但是可以以某种方式在xml资源中完成吗? In Android.manifest, or by referencing different resource values defined in res folders? 在Android.manifest中,还是通过引用res文件夹中定义的不同资源值?

Option 1: Use the following code for each activity inside AndroidManifest.xml: 选项1:对AndroidManifest.xml中的每个活动使用以下代码:

android:screenOrientation="nosensor"

The "nosensor" setting tells your app to use the natural orientation for the device, and will not change if the user rotates the device. “ nosensor”设置告诉您的应用使用设备的自然方向,如果用户旋转设备,则不会更改。 So tablets will use landscape orientation for your app, and phones will always use portrait orientation. 因此,平板电脑将对您的应用使用横向,而手机将始终使用纵向。

Next you can create a separate layout file for landscape native devices (ie tablets) by creating a folder called layout-land in your res folder. 接下来,您可以通过在res文件夹中创建一个名为layout-land的文件夹,为横向本机设备(即平板电脑)创建一个单独的布局文件。 Put the XML layout files for tablets into the layout-land folder. 将平板电脑的XML布局文件放入layout-land文件夹。

Option 2: Use the smallest width qualifier, as described in the Android developer documentation here: http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali 选项2:使用最小宽度限定符,如此处的Android开发者文档所述: http : //developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

In this case you will create a layout folder in the res directory called layout-sw600dp. 在这种情况下,您将在res目录中创建一个名为layout-sw600dp的布局文件夹。 Inside there you will put your alternate XML files for devices with more than 600dp. 在其中,您将为600dp以上的设备放置备用XML文件。 In the top level of the XML files for your linear layout, or relatively layout, for example, be sure to include the following: 例如,在线性布局或相对布局的XML文件的顶层,请确保包括以下内容:

android:orientation="horizontal"

In your main XML files for devices with less than 600dp, you will force them to vertical orientation: 在小于600dp的设备的主要XML文件中,您将强制其垂直放置:

android:orientation="vertical"

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

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