仅在横向模式下运行应用程序?
我想在我的设备上仅以横向模式运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过编程方式获取两者,如下所示:
或者在清单文件中,您可以在此处给出
You can go for both programmatically as follows:
or also in manifest file you can give here as
将其添加到清单文件中的活动标签中:
它将把您的方向固定为横向模式,并且您不需要向用户显示任何 toast。
add this to your activity tag in your manifest file:
It will fix your orientation to landscape mode and you need not to show any toast to user.
在您的 Manifestfile(xml) 中
也请参考此 是否可以检查是否因方向更改而调用了 onCreate?
in your Manifestfile(xml)
refer this also Is that possible to check was onCreate called because of orientation change?
你可以尝试这样的事情
You may try something like this
您可以使用
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).