所以我正在为 Android 制作一个应用程序,我想强制平板电脑横向方向和手机纵向方向。然而,似乎我只能根据我所看到的进行方向锁定,这违背了想要设备有两个单独方向的目的。
平板电脑:横向
手机:纵向
说得更技术一些。
我在“res/layout-xlarge-land”中有一个布局,用于在平板电脑上进行美化,并且在“res/layout”中有原始布局,我只想在平板电脑上明确使用layout-xlarge-land。没有别的,基本上只对大型设备使用横向。
谢谢!
So I'm making an application for Android and I want to force Landscape orientation for tablets and Portrait orientation for phones. However, it seems as though I can only do an orientation lock from what I've seen which defeats the purpose of wanting two separate orientations for devices.
Tablets: Landscape
Phones: Portrait
To put it more technical.
I have a layout in "res/layout-xlarge-land" for landscaping on the tablet and I have the original layout in "res/layout" and I just want to explicitly use layout-xlarge-land for the tablet. Nothing else, essentially ONLY using landscape for xlarge devices.
Thanks!
发布评论
评论(5)
根据设备密度设置特定方向可能不起作用,因为有些手机的密度比平板电脑更高。
我所做的是通过在清单文件中的活动标记中设置属性来禁用设备的方向传感器,如下所示:
当您运行应用程序时,默认情况下为手机设置纵向方向,为平板电脑设置横向方向(因此它将选择来自
layout-xlarge-land
的 xml 文件)。由于您设置了方向锁定,因此它会保持该方向。Setting a particular orientation based on device density may not work because there are phones which have higher densities than tablets.
What I did was to disable the device's orientation sensor by setting the attribute in the activity tag in the manifest file like this:
When you run your app, by default portrait orientation is set for phones and landscape for tablets(and hence it'll select the xml file from
layout-xlarge-land
). And since you've set an orientation lock, it remains in this orientation.您可以测量设备的实际尺寸(以英寸为单位),然后使用以下方式以编程方式设置方向:
或者
要获取设备的物理尺寸,您可以使用发布的代码 这里。
它不是 100% 准确,但足以确定哪个设备正在运行该应用程序。
You can measure the actual size (in inches) of the device and then programmatically set the orientation using:
or
To get the physical size of the device you can use the code published here.
It is not 100% accurate, but it is good enough to decide what king of device is running the app.
我不完全确定你在说什么,但看来你应该衡量分辨率并采取相应的行动。我的意思是,除了分辨率不同之外,你怎么知道它是手机还是平板电脑?
I'm not entirely sure what you're saying, but it would seem that you should just measure the resolution and act accordingly. I mean, how do you know if it's a phone or a tablet OTHER than the resolution being different?
对于使用过但没有单独使用它的人,
除了我的基本活动中的此代码片段之外,我还使用了它
for people who used
and did not work on it own I used it in addition to this snippet in my base activity
在“layout-large”文件夹布局的布局菜单上设置名为 llTablet 的布局,并在“layout”文件夹的布局菜单上设置名为 llPhone 的布局。菜单指的是用户输入的第一个布局。然后检查是否可以引用它。如果您无法引用 llPhone,那么它就是平板电脑。
set a layout called llTablet on the layout menu in the "layout-large" folder layouts and a llPhone layout on the layout menu in the "layout" folder. By menu am refering to the first layout the user is entering. Then check if you can reference it. If you can't reference llPhone then its a tablet.