手机纵向,平板电脑横向(Android 布局)

发布于 2024-12-08 12:44:32 字数 271 浏览 1 评论 0 原文

所以我正在为 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

私野 2024-12-15 12:44:32

根据设备密度设置特定方向可能不起作用,因为有些手机的密度比平板电脑更高。

我所做的是通过在清单文件中的活动标记中设置属性来禁用设备的方向传感器,如下所示:

android:screenOrientation="nosensor"

当您运行应用程序时,默认情况下为手机设置纵向方向,为平板电脑设置横向方向(因此它将选择来自 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:

android:screenOrientation="nosensor"

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.

溺渁∝ 2024-12-15 12:44:32

您可以测量设备的实际尺寸(以英寸为单位),然后使用以下方式以编程方式设置方向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

或者

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

要获取设备的物理尺寸,您可以使用发布的代码 这里

它不是 100% 准确,但足以确定哪个设备正在运行该应用程序。

You can measure the actual size (in inches) of the device and then programmatically set the orientation using:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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.

那些过往 2024-12-15 12:44:32

我不完全确定你在说什么,但看来你应该衡量分辨率并采取相应的行动。我的意思是,除了分辨率不同之外,你怎么知道它是手机还是平板电脑?

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?

要走干脆点 2024-12-15 12:44:32

对于使用过但没有单独使用它的人,

android:screenOrientation="nosensor"

除了我的基本活动中的此代码片段之外,我还使用了它

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            && (this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }else if((this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    super.onCreate(savedInstanceState);
}

for people who used

android:screenOrientation="nosensor"

and did not work on it own I used it in addition to this snippet in my base activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            && (this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }else if((this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    super.onCreate(savedInstanceState);
}
紅太極 2024-12-15 12:44:32

在“layout-large”文件夹布局的布局菜单上设置名为 llTablet 的布局,并在“layout”文件夹的布局菜单上设置名为 llPhone 的布局。菜单指的是用户输入的第一个布局。然后检查是否可以引用它。如果您无法引用 llPhone,那么它就是平板电脑。

llPhone = (LinearLayout) findViewById (R.id.llPhone)
if (llPhone == null) {
 tablet = true;
}
else {
 tablet = false;
}

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.

llPhone = (LinearLayout) findViewById (R.id.llPhone)
if (llPhone == null) {
 tablet = true;
}
else {
 tablet = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文