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中添加android:configchanges=“orientation”。当我的应用程序启动时,我的屏幕是纵向的,我按ctrl+f12,屏幕也是横向的,屏幕是横向的,第二次按ctrl+f12,屏幕也是横向的,屏幕是纵向的,然后按ctrl+f12,屏幕保持纵向。所以我再按一次,屏幕保持肖像。我的问题是为什么我的屏幕在应用程序启动时不保持纵向。
编辑:我想用代码控制屏幕漫游,如果我能做到这一点?

最佳答案

如果您试图使应用程序始终处于纵向模式,则可以此行指向清单中的元素

android:screenOrientation="portrait"

在androidmanifest.xml文件中的每个活动中添加这一行。

09-30 15:04
查看更多