仅在横向模式下运行应用程序?

发布于 2024-12-06 11:46:25 字数 110 浏览 1 评论 0原文

我想在我的设备上仅以横向模式运行 Hello World 示例应用程序。

如果设备改为纵向,我就想举起一台烤面包机。例如“请更改为横向模式来运行您的应用程序”。

我该怎么做?

I want to run a Hello World sample application only in landscape mode on my Device.

If the device changed to portrait, I would then like to raise one toaster. For example "please change to landscape mode to run your application".

How should I do this?

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

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

发布评论

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

评论(5

一身软味 2024-12-13 11:46:25

您可以通过编程方式获取两者,如下所示:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

或者在清单文件中,您可以在此处给出

<activity android:name=".YourActivity"  android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/>

You can go for both programmatically as follows:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

or also in manifest file you can give here as

<activity android:name=".YourActivity"  android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/>
山有枢 2024-12-13 11:46:25

将其添加到清单文件中的活动标签中:

android:screenOrientation="landscape"

它将把您的方向固定为横向模式,并且您不需要向用户显示任何 toast。

add this to your activity tag in your manifest file:

android:screenOrientation="landscape"

It will fix your orientation to landscape mode and you need not to show any toast to user.

酒浓于脸红 2024-12-13 11:46:25

在您的 Manifestfile(xml) 中

<activity android:name=".ActivityName" android:screenOrientation="landscape">
    </activity>

也请参考此 是否可以检查是否因方向更改而调用了 onCreate?

in your Manifestfile(xml)

<activity android:name=".ActivityName" android:screenOrientation="landscape">
    </activity>

refer this also Is that possible to check was onCreate called because of orientation change?

耀眼的星火 2024-12-13 11:46:25

你可以尝试这样的事情

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

Log.v("log_tag", "display width is "+ width);
Log.v("log_tag", "display height is "+ height);

if(width<height){
    Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
} else {
    Toast.makeText(getApplicationContext(),"Device is in landscape  mode",Toast.LENGTH_LONG ).show();
}

You may try something like this

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

Log.v("log_tag", "display width is "+ width);
Log.v("log_tag", "display height is "+ height);

if(width<height){
    Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
} else {
    Toast.makeText(getApplicationContext(),"Device is in landscape  mode",Toast.LENGTH_LONG ).show();
}
巴黎夜雨 2024-12-13 11:46:25

您可以使用 configChanges - 您想要捕获屏幕方向变化的 Activity 的属性。

之后, onConfigurationChanged()-method将被调用。

要检查当前显示的方向,请查看这篇旧文章:检查 Android 手机上的方向

之后,您可以展示您的消息或做任何您喜欢的事情(当然您也可以将其设置为仅以横向显示)。

You can use the configChanges-attribute for the Activity you want to catch the screen-orientation change for.

After that, the onConfigurationChanged()-method in your Activity will be called when the orientation changes.

To check which orientation is currently displayed, check this older post: Check orientation on Android phone

After that, you can show off your message or do whatever you like (of course you can also set it to only show in landscape).

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