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);
}
}
}
我的應用程序,並添加機器人:在xml.when configChanges =「方向」我的應用程序開始我的屏幕是人像,我按下CTRL + F12,屏幕也roate ,屏幕是LANDSCAPE,第二我按Ctrl + F12,屏幕也旋轉,屏幕是PORTRAIT,然後我按下Ctrl + F12,屏幕保持PORTRAIT。所以我再次按下,屏幕保持PORTRAIT。我的問題是,爲什麼我的屏幕在應用程序啓動時不保持PORTRAIT。禁用屏幕旋轉在Android如上
編輯:我想用代碼來控制屏幕旋轉,如果我能做到這一點?
感謝您answer.i還我可以用代碼來保持屏幕肖像 – pengwang 2011-04-06 09:54:49
@pengwang它是否解決問題了嗎?如果是這樣,請接受它作爲答案。 – pecka85 2011-04-11 08:48:54
不,我找不到方法 – pengwang 2011-04-11 09:32:53