2011-04-06 105 views
3
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如上

编辑:我想用代码来控制屏幕旋转,如果我能做到这一点?

回答

15

如果你试图让你的应用程序是在人像模式中的所有的时间,你可以把这个信号的元素在清单

android:screenOrientation="portrait" 

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

+0

感谢您answer.i还我可以用代码来保持屏幕肖像 – pengwang 2011-04-06 09:54:49

+0

@pengwang它是否解决问题了吗?如果是这样,请接受它作为答案。 – pecka85 2011-04-11 08:48:54

+0

不,我找不到方法 – pengwang 2011-04-11 09:32:53

4

更改AndroidManifest.xml文件。这就够了。现在

<activity android:name="Activity_Name" android:screenOrientation="portrait"/>

您的应用程序不会改变方向。

+0

感谢您的回答,我希望在运行时使用代码禁用屏幕旋转。 – pengwang 2011-04-06 09:55:44

7

我相信你正在寻找的方法是Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_<PORTRAIT/LANDSCAPE>)

这与

android:configChanges="orientation"

在显明活动告诉你将处理方向,系统自己应该禁用自动重新定向在一起。

+1

我已经解决了这个问题,但我不需要android:configChanges =“orientation” – pengwang 2011-04-11 09:46:18

0

您也可以在运行时从Java源代码禁用屏幕旋转。
刚刚尝试这种方式

private void setAutoRotation(final boolean autorotate) { 
     AsyncTask.execute(new Runnable() { 
      public void run() { 
       try { 
        IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE)); 
        if (autorotate) { 
         wm.thawRotation(); 
        } else { 
         wm.freezeRotation(-1); 
        } 
       } catch (RemoteException exc) { 
        Log.secW(TAG, "Unable to save auto-rotate setting"); 
       } 
      } 
     }); 
    } 

现在试试你的应用程序在运行时调用此方法。可能在某些方法或某些按钮上。

setAutoRotation(false); 

setAutoRotation(true);