Lock Android phone application to Portrait mode

夙愿已清 提交于 2019-11-27 10:12:38

问题


Can someone tell me how to lock my application to a portrait mode? Is it a simple configuration in the manifest file?


回答1:


Yes. Add android:screenOrientation="portrait" to the manifest under your main activity.

<activity android:name=".yourActivity" android:screenOrientation="portrait"... />



回答2:


Yes! It's an attribute of the activity tag:

<activity android:name=".yourActivity" android:screenOrientation="portrait" ... />



回答3:


Also, you may have to add the following to your activity element:

android:configChanges="keyboardHidden"

That way, the OS won't change the orientation when the user opens a sliding keyboard.




回答4:


None of these answers worked on my system but I did find the following worked perfectly for a simple app that I developed:

Within MainActivity.java add:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

to onCreate ()

This is mine:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

I know that it is not (always) best practice locking orientation, but in special circumstances it is valid and I only want this temporarily while I continue developing.



来源:https://stackoverflow.com/questions/5044544/lock-android-phone-application-to-portrait-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!