我们如何根据设备设置 Android 中 Activity 的方向?

发布于 2025-01-04 14:04:43 字数 98 浏览 0 评论 0原文

我有一种情况,如果 Android 设备是平板电脑,那么我希望启用方向(纵向和横向),而如果设备是手机,我希望方向仅处于纵向模式。 你能帮我怎么做吗?

谢谢, 尼辛

I have a situation, wherein, if the Android device is a tablet, then I want the orientation to be enabled (portrait and landscape), whereas if the device is a phone, I want the orientation to be only in Portrait mode.
Can you help me how to do this?

Thanks,
Nithin

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

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

发布评论

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

评论(5

青春有你 2025-01-11 14:04:43

您有 2 种方法:

a) 分别为手机和平板电脑创建不同的布局和绘图集,并根据您需要的方向进行设计。

b) 在运行时检查操作设备并设置方向。要检查设备,请检查以下参数并以编程方式设置方向,

  (1) Build.VERSION.SDK_INT
  (2) Configuration.SCREENLAYOUT_SIZE_MASK

You have 2 methods :

a) Create different sets of layout and drawables for the phone and tablet separately and design them according to the orientation you need.

b) Check for the operating device during runtime and set the orientation. To check the device , give a check on the following parameters and set the orientation programatically,

  (1) Build.VERSION.SDK_INT
  (2) Configuration.SCREENLAYOUT_SIZE_MASK
笑忘罢 2025-01-11 14:04:43

试试这个
它非常适合我......

if ( isXLargeScreen() )
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

并且
这个函数用来决定平板电脑和手机,在上面的 IF 中调用......

public static boolean isXLargeScreen(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
} 

try this
It works perfectly for me.....

if ( isXLargeScreen() )
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

and
this function to decide between tablet and phones,Called in the IF above.....

public static boolean isXLargeScreen(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
} 
把昨日还给我 2025-01-11 14:04:43

您可以使用这个类来获取有关当前版本的设备信息,从系统属性中提取。

要设置方向属性,请使用以下属性...

android:screenOrientation=["unspecified" | "user" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "sensor" | "fullSensor" | "nosensor"]

You can use this class to get the device information about the current build, extracted from system properties.

To set the orientation properties, use following attribute...

android:screenOrientation=["unspecified" | "user" | "behind" |
                                     "landscape" | "portrait" |
                                     "reverseLandscape" | "reversePortrait" |
                                     "sensorLandscape" | "sensorPortrait" |
                                     "sensor" | "fullSensor" | "nosensor"]
桜花祭 2025-01-11 14:04:43

我自己只是一个初学者,但也许可以通过针对特定的 API 级别或 dpi 来做到这一点?您可以为您所定位的不同设备提供备用资源。

例如:

res/layout/main.xml
res/layout-xhdpi/main.xml
res/layout-v11/main.xml

可以在此处找到一个很好的文档

I'm just a beginner at this myself, but perhaps it'd be possible to do this by targeting a specific API level or dpi? You can provide alternate resources for the different devices you are targeting.

For example:

res/layout/main.xml
res/layout-xhdpi/main.xml
res/layout-v11/main.xml

A good documentation for that can be found here.

迷你仙 2025-01-11 14:04:43

获取设备屏幕的宽度和高度,如果分辨率小于平板电脑,则可以将应用程序的方向设置为纵向。

如果需要 Activity 的显示尺寸(以像素为单位),您可以使用 getSize:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

如果不是 Activity,您可以通过 WINDOW_SERVICE 获取默认显示:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

if(width<= (resolution width of tablet) && height<=(resolution height of tablet))
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
}

不要在清单中基于以下内容给出任何命令

android:屏幕方向

get the width and height of the device screen, if the resolution is less than that of tablet, then you can set the orientation of the app to portrait.

If need the display dimensions in pixels of activity, you can use getSize:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If not an Activity you can get the default Display through WINDOW_SERVICE:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

if(width<= (resolution width of tablet) && height<=(resolution height of tablet))
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
}

Dont give any command in manifest based on

android:screenOrientation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文