Fix orientation of screen on Dialog view

淺唱寂寞╮ 提交于 2019-12-12 04:21:12

问题


I am using a custom dialog in my activity. I want to set the screen orientation only if the dialog is shown elsewhere the screen orientation can change to portrait to landscape vice versa. Is there any way to fix the orientation for such particular case specifically in java code.


回答1:


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Show dialog here
//...
//Hide dialog here
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);



回答2:


We had to do this in a fullscreen dialog launched from a service.

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
params.copyFrom(window.getAttributes());
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

window.setAttributes(params);



回答3:


use android:configChanges="orientation" then Android doesn't re-create the activity when the orientation is changed. If you don't want the dialog to be dismissed, just use the Activity.showDialog() method. And if you still want to use android:configChanges="orientation" then you have to change drawables manually in the Activity.onConfigurationChanged() method.



来源:https://stackoverflow.com/questions/12457352/fix-orientation-of-screen-on-dialog-view

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