Android:当屏幕小于 x 时禁用屏幕旋转

发布于 2024-12-04 03:59:42 字数 138 浏览 7 评论 0原文

有人知道当屏幕小于(例如)480px 时如何在 Android 应用程序中禁用屏幕旋转吗? 我正在使用 PhoneGap 构建应用程序,该应用程序将针对平板电脑设备,但也可以在智能手机上运行。不幸的是,只有当应用程序以横向显示时,应用程序才能正确显示......

Anyone know how can I disable screen rotation in my Android app when screen is smaller than (for example) 480px?
I'm building app using phonegap witch will be targeted to tablet devices, but it's also possible to run it on smart-phones. Unfortunately application is only displayed properly when app is displayed in landscape orientation...

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

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

发布评论

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

评论(3

漫雪独思 2024-12-11 03:59:42

也许是一个“if”控制的

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

在 onCreate 函数中?

Maybe an 'if' controlled

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

in the onCreate function?

坦然微笑 2024-12-11 03:59:42

做到了! :)

以下是方法(不言自明):

int width;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
    Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();
    if(width <= 480) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

}


@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(width <= 480) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}'

以及一些导入:

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Display;'

Did it! :)

Here is how (self explanatory):

int width;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.loadUrl("file:///android_asset/www/index.html");
    Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();
    if(width <= 480) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

}


@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(width <= 480) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}'

and few imports:

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Display;'
烂柯人 2024-12-11 03:59:42

结合@Howard Hodson的解决方案和这个线程如何使用代码确定设备屏幕尺寸类别(小、正常、大、超大)?

我使用以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    disableLandscapeInSmallDevices();
}

private void disableLandscapeInSmallDevices() {
    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

combining the solution of @Howard Hodson and in this thread How to determine device screen size category (small, normal, large, xlarge) using code?

I use this code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    disableLandscapeInSmallDevices();
}

private void disableLandscapeInSmallDevices() {
    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文