禁用屏幕旋转在Android上运行 [英] Disable screen rotation at runtime on Android
问题描述
public class testScreenRotation extends Activity {
/** Called when the activity is first created. */
private int mRuntimeOrientation;
private boolean mDisableScreenRotation=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRuntimeOrientation = this.getScreenOrientation();
setContentView(R.layout.main);
}
protected int getScreenOrientation() {
/*
Display display = getWindowManager().getDefaultDisplay();
int orientation = display.getOrientation();
if (orientation == Configuration.ORIENTATION_UNDEFINED) {
orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_UNDEFINED) {
if (display.getWidth() == display.getHeight())
orientation = Configuration.ORIENTATION_SQUARE;
else if(display.getWidth() < display.getHeight())
orientation = Configuration.ORIENTATION_PORTRAIT;
else
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
*/
return Configuration.ORIENTATION_PORTRAIT;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
if (mDisableScreenRotation) {
super.onConfigurationChanged(newConfig);
this.setRequestedOrientation(mRuntimeOrientation);
} else {
mRuntimeOrientation = this.getScreenOrientation();
super.onConfigurationChanged(newConfig);
}
}
}
我的应用程序如上面,然后加入Android:在xml.when configChanges =方向我的应用程序开始我的屏幕是人像,我preSS CTRL + F12,屏幕也roate,屏幕为横向,第二我preSS CTRL + F12,屏幕也roate,屏幕人像,然后我preSS CTRL + F12,屏幕保持肖像。所以我再次preSS,画面保持肖像。我的问题是,为什么我的屏幕跟不上肖像时,应用程序启动。
my app as above ,and add android:configChanges="orientation" in the xml.when my app start my screen is PORTRAIT,i press ctrl+F12,the screen also roate,the screen is LANDSCAPE,second i press ctrl+F12,the screen also roate,the screen is PORTRAIT,then i press ctrl+F12,the screen keep PORTRAIT. so i press again,the screen keep PORTRAIT. my question is that why my screen not keep PORTRAIT when the app start.
编辑:我想用code来控制屏幕roate,如果我能做到这一点。
i want to use code to control the screen roate, if i can do this?
推荐答案
如果你试图让你的应用程序是在人像模式中的所有时间,可以将此行的元素在清单
If you are trying to get your application to be in Portrait-mode all the time, you can this line to the element in the manifest
android:screenOrientation="portrait"
在每个活动添加这条线在AndroidManifest.xml文件。
Add This line in each activity in the AndroidManifest.xml file.
这篇关于禁用屏幕旋转在Android上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!