Android - 仅限垂直布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2504064/
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
Android - Vertical layout only
提问by teepusink
How do I make sure my app is only for vertical layout?
如何确保我的应用仅适用于垂直布局?
I have tried android:screenOrientation="portrait"
but that doesn't seem to do the trick.
我试过了,android:screenOrientation="portrait"
但这似乎不起作用。
回答by Pentium10
You need to add to all your activity not for one only. I think you understood that setting is per application wide, but it isn't.
您需要添加到您的所有活动中,而不仅仅是一项活动。我想你明白设置是针对每个应用程序的,但事实并非如此。
<activity android:name=".MyActivity"
android:label="My Activity"
android:screenOrientation="portrait">
Add the declaration to the activity tag in AndroidManifest for every Activity you want to be portrait-only.
将声明添加到 AndroidManifest 中的活动标记中,为您希望仅显示纵向的每个活动。
回答by ddmytrenko
If you want some group of your activities to be locked on PORTRAIT mode only, than you can choose the next way:
如果您只想将某些活动组锁定在肖像模式下,您可以选择下一种方式:
public abstract class BasePortraitActivity extends Activity {
@Override
protected final void onCreate(Bundle state) {
super.onCreate(state);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
performOnCreate(state);
}
protected abstract void performOnCreate(Bundle state);
}
And than just extend BasePortraitActivity
where you need it. Or just add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
to YourActivity.onCreate()
.
而不仅仅是扩展BasePortraitActivity
到您需要的地方。或者只是添加setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
到YourActivity.onCreate()
.
回答by Sameer Kulkarni
you have to change into AndroidManifest.xml.
你必须改成 AndroidManifest.xml。
for each activity you have to insert:
对于您必须插入的每个活动:
android:configChanges = "orientation"
android:screenOrientation = "portrait"
for e.g.:
例如:
<activity android:name=".YourActivityName"
android:label="@string/app_name"
android:configChanges = "orientation"
android:screenOrientation = "portrait">
This works for a single activity.. But there seems no application wide setting there.
这适用于单个活动。但似乎没有应用范围的设置。
回答by Zeus
In your manifest under activity add this :
在活动下的清单中添加以下内容:
<activity
android:name="com.zeus.MyProject"
android:screenOrientation="portrait"
>
回答by Michel Silva
Activity is to block just in case want to block all and only repeat this line of code in their Activitys.
Activity 就是阻塞以防万一想要阻塞所有并且只在他们的 Activity 中重复这行代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Here, talk to the java does not want that the user can rotate their activity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
or open your "AndroidManisfest.xml" and add the rows for the portrait mode as shown below.
或打开您的“AndroidManisfest.xml”并为纵向模式添加如下所示的行。
android:configChanges="orientation"
android:screenOrientation="portrait">
回答by Chris
Add android:configChanges="keyboardHidden|orientation"
to the activity.
添加android:configChanges="keyboardHidden|orientation"
到活动中。
回答by Bill
Just to confirm Pentium10's answer. I was having a similar issue and adding android:screenOrientation="portrait" to the tag did the trick for me.
只是为了确认 Pentium10 的回答。我遇到了类似的问题,将 android:screenOrientation="portrait" 添加到标签中对我有用。
回答by Kantesh
android:orientation="vertical" put this attribute in layout root,try it it may help.
android:orientation="vertical" 将此属性放在布局根目录中,尝试它可能会有所帮助。