问题
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