Java 如何将我的 android 程序的布局锁定到一个方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I lock the layout of my android program to one orientation
提问by Mike V
Possible Duplicate:
Don't want activity to be destroyed/created when phone is rotated
可能的重复:
不希望手机旋转时活动被破坏/创建
I'm trying to write a program that needs smooth orientation sensor samples as the phone is rotated all around. Every time the orientation of the phone is changed, it interrupts the data and freezes the program while the layout changes. How can I lock it in one orientation (landscape or portrait, it doesn't matter)?
我正在尝试编写一个程序,当手机四处旋转时,该程序需要平滑的方向传感器样本。每次更改手机的方向时,它都会在布局更改时中断数据并冻结程序。如何将其锁定在一个方向(横向或纵向,无所谓)?
I assume it has something to do with android:layout
in the xml file, but I can't find anything online.
我认为它与android:layout
xml 文件有关,但我在网上找不到任何东西。
采纳答案by Andreas Dolk
This answerlooks promising to me (used [android] [screen-orientation]
in the SO search box)
这个答案对我来说很有希望([android] [screen-orientation]
在 SO 搜索框中使用)
回答by Samuh
Simplest and one of ways to do this would be having android:screenOrientation="portrait|landscape"
property for the <activity>
tag in your AndroidManifest.xml
file.
最简单的方法之一就是android:screenOrientation="portrait|landscape"
为文件中的<activity>
标签设置属性AndroidManifest.xml
。
回答by andy_spoo
The above never seems to work for me.
以上似乎对我不起作用。
I always use:-
我总是使用:-
setRequestedOrientation(0);
setRequestedOrientation(0);
Changing the value at the end will give horizintal/vertical lock. Make sure to catch the keyboard being opened in the manifest though, if in portrait mode, as it may give unexpected results.
更改最后的值将提供水平/垂直锁定。但是,如果在纵向模式下,请确保在清单中找到正在打开的键盘,因为它可能会产生意想不到的结果。
回答by murali.dhuli
You can use this syntax in the manifest file for a particular activity:
您可以在清单文件中为特定活动使用此语法:
android:configChanges="keyboardHidden|orientation"
回答by Jakub Ku?era
In java code it will look like this:
在 Java 代码中,它将如下所示:
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);